Change MigrationsHistoryTable column names in EF Core

我的梦境 提交于 2019-12-19 10:22:47

问题


I have a standardized all table and column names in my EF Core database to use snake_case. I was able to change the migrations history table name and schema to match the rest of the database, but I am not able to find a way to change the columns from MigrationId to migration_id and ProductVersion to product_version.

Any ideas on how this could be done?


回答1:


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>();


来源:https://stackoverflow.com/questions/41449653/change-migrationshistorytable-column-names-in-ef-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!