Change MigrationsHistoryTable column names in EF Core

混江龙づ霸主 提交于 2019-12-01 11:00:48

Here is an example of how to do it on SQL Server.

First, create a custom implementation of SqlServerHistoryRepository overriding ConfigureTable.

class MyHistoryRepository : SqlServerHistoryRepository
{
    public MyHistoryRepository(
        IDatabaseCreator databaseCreator, IRawSqlCommandBuilder rawSqlCommandBuilder,
        ISqlServerConnection connection, IDbContextOptions options,
        IMigrationsModelDiffer modelDiffer,
        IMigrationsSqlGenerator migrationsSqlGenerator,
        IRelationalAnnotationProvider annotations,
        ISqlGenerationHelper sqlGenerationHelper)
        : base(databaseCreator, rawSqlCommandBuilder, connection, options,
            modelDiffer, migrationsSqlGenerator, annotations, sqlGenerationHelper)
    {
    }

    protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
    {
        base.ConfigureTable(history);

        history.Property(h => h.MigrationId).HasColumnName("migration_id");
        history.Property(h => h.ProductVersion).HasColumnName("product_version");
    }
}

Then replace the replace the service with your custom implementation.

protected override void OnConfiguring(DbContextOptionsBuilder options)
    => options
        .UseSqlServer(connectionString)
        .ReplaceService<SqlServerHistoryRepository, MyHistoryRepository>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!