更改表名“__MigrationsHistory”?

时间:2017-06-19 20:54:07

标签: entity-framework oracle10g ef-migrations ef-code-first-mapping

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

2 个答案:

答案 0 :(得分:0)

请参阅此链接 - Is changing the name of the EF __Migration History table dangerous?

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

答案 1 :(得分:0)

这有点晚了,但是可以帮助使用同一数据库方案的多个DBContexts挣扎的人。

为了重命名__MigrationHistory表;创建一个实现HistoryContext类并覆盖父类OnModelCreating方法的自定义类。然后创建一个自定义配置类,并使用HistoryContext方法传递自定义SetDefaultHistoryContext

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

自定义HistoryContext类;

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类;

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