2

Is it possible to change table name "__MigrationsHistory" when using codefIrst? Problem: I am using Oracle Database and I have rules to create new tables. One of them is that there can not be any table names or fields with special characters.

2 Answers2

0

Refer this Link - Is changing the name of the EF __Migration History table dangerous?

This will explain on how to rename the database and what should be done.

ISHIDA
  • 4,700
  • 2
  • 16
  • 30
  • In the case shown in the link above was created a scheme, and there was no change in the name of the table histories. The problem faced is the special "__" characters in the table name because we use Oracle database and our DBAs are not able to handle this. To avoid friction between teams I thought I could rename the table "__MigrationsHistory" to "MigrationsHistory" or any other name that does not contain special characters. – Heitor Fernandes Neto Sep 20 '17 at 14:44
0

This is a bit late but, could help someone who struggle having multiple DBContexts using the same DB scheme.

In order to rename __MigrationHistory table; create a custom class that implements HistoryContext class and override parents OnModelCreating method. Then create a custom configuration class, pass our custom HistoryContext with SetDefaultHistoryContext method.

Please take a look at System.Data.Entity.Migrations.History.HistoryContext

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

Create a custom 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));
    }
}