Change MigrationsHistoryTable column names in EF Core

后端 未结 1 1770
栀梦
栀梦 2021-01-15 17:32

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 res

相关标签:
1条回答
  • 2021-01-15 18:00

    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>();
    
    0 讨论(0)
提交回复
热议问题