I have the code that map the __MigrationHistory to the existing database table.
namespace Alvin_CMS.Migrations
{
public class CustomHistoryContext : HistoryContext
{
public CustomHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "dbo");
//modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");
}
}
public class CustomHistoryConfiguration : DbConfiguration
{
public CustomHistoryConfiguration()
{
this.SetHistoryContext("System.Data.SqlClient",
(connection, defaultSchema) => new CustomHistoryContext(connection, "dbo"));
}
}
}
Also I have another custom history context:
namespace EAccounting.Migrations
{
public class CustomHistoryContext : HistoryContext
{
public CustomHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "EAccounting");
//modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");
}
}
public class CustomHistoryConfiguration : DbConfiguration
{
public CustomHistoryConfiguration()
{
this.SetHistoryContext("System.Data.SqlClient",
(connection, defaultSchema) => new CustomHistoryContext(connection, "EAccounting"));
}
}
}
And I call this in my code:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<EAccounting.Models.EAccountingMigrationDBContext, EAccounting.Migrations.Configuration>());
I have multiple databases which have their own custom migration history context. How can I let the database initializer for the migration history to choose which history context they will use?