在EF Core中更改MigrationsHistoryTable列名称 [英] Change MigrationsHistoryTable column names in EF Core
问题描述
我在我的EF Core数据库中具有标准化的所有表名和列名,以使用snake_case。我能够更改迁移历史记录表的名称和架构以匹配数据库的其余部分,但是我无法找到一种方法将列从 MigrationId
更改为 migration_id
和 ProductVersion
到 product_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.
首先,创建 SqlServerHistoryRepository
的自定义实现,重写 ConfigureTable
。
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列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!