在EF Core中更改MigrationsHistoryTable列名稱

entity-framework-core

我在EF Core數據庫中有一個標準化的所有表和列名稱,以使用snake_case。我能夠更改遷移歷史記錄表名稱和架構以匹配數據庫的其餘部分,但我無法找到將MigrationId的列更改為migration_idProductVersionproduct_version

關於如何做到這一點的任何想法?

一般承認的答案

以下是如何在SQL Server上執行此操作的示例。

首先,創建一個覆蓋ConfigureTableSqlServerHistoryRepository的自定義實現。

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");
    }
}

然後用您的自定義實現替換替換服務。

protected override void OnConfiguring(DbContextOptionsBuilder options)
    => options
        .UseSqlServer(connectionString)
        .ReplaceService<SqlServerHistoryRepository, MyHistoryRepository>();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow