【问题标题】:How to Customize Migration History Table with Entity Framework Core如何使用 Entity Framework Core 自定义迁移历史记录表
【发布时间】:2019-08-15 23:13:46
【问题描述】:

我正在新 API 中设置 Entity Framework Core,以部署到 Entity Framework 4.6 应用程序使用的现有 SQL Server 数据库。有一个由其他应用程序共享的迁移历史表,其中有 2 个字段需要为每个条目填充:ContextKey 和 Model。 Entity Framework Core 没有上下文键,也没有将模型保存到迁移历史表中。

我已经创建了一个 HistoryRepository : SqlServerHistoryRepository 并配置了 Entity Framework Core 以使用它,但是 ConfigureTable 方法只允许您创建其他列,但实际上并不能在插入自定义数据时填充每条记录。为列提供默认值不是解决方案。

public class HistoryRepository : SqlServerHistoryRepository
{
    public HistoryRepository(HistoryRepositoryDependencies dependencies)
        : base(dependencies)
    {
    }
    protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
    {
        base.ConfigureTable(history);
        history.Property<string>("ContextKey")
            .HasMaxLength(300);
        history.Property<byte[]>("Model");
    }
}

 services.AddDbContext<MDSContext>(options =>
     options.UseLazyLoadingProxies()
            .UseSqlServer(
                 connectionString,
                 x => x.MigrationsHistoryTable("__MigrationHistory")).ReplaceService<Microsoft.EntityFrameworkCore.Migrations.IHistoryRepository, Burkhart.CoreServices.IncomingOrders.Core.Models.Base.HistoryRepository>()
 );

我应该能够动态地为 ContextKey 和 Model 提供自定义值

【问题讨论】:

    标签: entity-framework-core entity-framework-migrations entity-framework-core-migrations


    【解决方案1】:

    我到处寻找解决方案,但它们都向您展示了如何添加列和设置默认值,而不是如何动态设置值。我最终在 GitHub 上挖掘了 ASP.NET Entity Framework Core 源代码以获取解决方案,以便与其他人分享它,因为我知道还有其他人正在寻找此信息:

    只需覆盖 HistoryRepository 上的 GetInsertScript 方法并插入您的自定义值。这是完整的解决方案:

    public class HistoryRepository : SqlServerHistoryRepository
    {
        public HistoryRepository(HistoryRepositoryDependencies dependencies)
            : base(dependencies)
        {
        }
        protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
        {
            base.ConfigureTable(history);
            history.Property<string>("ContextKey")
                .HasMaxLength(300);
            history.Property<byte[]>("Model");
        }
        public override string GetInsertScript(HistoryRow row)
        {
            var stringTypeMapping = Dependencies.TypeMappingSource.GetMapping(typeof(string));
            return new StringBuilder().Append("INSERT INTO ")
                .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))
                .Append(" (")
                .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName))
                .Append(", ")
                .Append(SqlGenerationHelper.DelimitIdentifier(ProductVersionColumnName))
                .Append(", [ContextKey], [Model])")
                .Append("VALUES (")
                .Append(stringTypeMapping.GenerateSqlLiteral(row.MigrationId))
                .Append(", ")
                .Append(stringTypeMapping.GenerateSqlLiteral(row.ProductVersion))
                .Append($", '{ContextConstants.ContextName}.{ContextConstants.ContextSchemaName}', 0x)")
                .AppendLine(SqlGenerationHelper.StatementTerminator)
                .ToString();
        }
    }
    

    这里是github上的源代码链接: https://github.com/aspnet/EntityFrameworkCore/blob/master/src/EFCore.Relational/Migrations/HistoryRepository.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2017-10-27
      • 2018-03-21
      • 2020-08-10
      • 2018-11-05
      相关资源
      最近更新 更多