更改 table 名称“__MigrationsHistory”?

Change table name "__MigrationsHistory"?

使用 codefIrst 时是否可以更改 table 名称“__MigrationsHistory”? 问题:我正在使用 Oracle 数据库并且我有创建新 table 的规则。其中之一是不能有任何 table 名称或带有特殊字符的字段。

参考这个 Link -

这将解释如何重命名数据库以及应该做什么。

这有点晚了,但是可以帮助那些在使用相同的数据库方案时遇到多个 DBContexts 困难的人。

为了重命名__MigrationHistorytable;创建一个实现 HistoryContext class 并覆盖父 OnModelCreating 方法的自定义 class。然后创建一个自定义配置 class,通过 HistoryContextSetDefaultHistoryContext 方法传递我们的自定义 HistoryContext

请看System.Data.Entity.Migrations.History.HistoryContext

自定义 HistoryContext class;

public class YourHistoryContext : HistoryContext
{
    public YourHistoryContext(System.Data.Common.DbConnection dbConnection, string defaultSchema)
    : base(dbConnection, defaultSchema)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<HistoryRow>().ToTable(tableName: "YourCustomMigrationHistory"/*, schemaName: "dbo__OrYourCustomScheme"*/);

        //Rename Id column name.
        //modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");
    }
}

创建自定义 DbConfiguration class;

public class MigrationHistoryConfiguration : DbConfiguration
{
    public MigrationHistoryConfiguration()
    {
        //this.SetHistoryContext("System.Data.SqlClient",
        //    (connection, defaultSchema) => new HistoryContext(connection, defaultSchema));
        this.SetDefaultHistoryContext((connection, defaultSchema) => new YourHistoryContext(connection, defaultSchema));
    }
}