MigrationsHistoryTable

MigrationsHistoryTable

本文介绍了更改 EF Core 中的 MigrationsHistoryTable 列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 EF Core 数据库中有一个标准化的所有表和列名称来使用 snake_case.我能够更改迁移历史记录表名称和架构以匹配数据库的其余部分,但我无法找到将列从 MigrationId 更改为 migration_idProductVersionproduct_version.

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?

推荐答案

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

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

首先,创建覆盖 ConfigureTableSqlServerHistoryRepository 的自定义实现.

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

这篇关于更改 EF Core 中的 MigrationsHistoryTable 列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:22