Each instance of your context has one database connection.
Assuming that each child database wille have the same code-first model, you can launch one instance of the same context class for each database.
Just call DbContext.Initialize( true ) to migrate the database, then close the connection.
var context1 = new MigratorContext( connectionString1 );
context1.Initilialize( true );
var context2 = new MigratorContext( connectionString2 );
context2.Initilialize( true );
var context3 = new MigratorContext( connectionString3 );
context3.Initilialize( true );
Add a constructor for MigratorContext taking the connection string:
public MigratorContext(string connString)
: base( connString)
{
var migrationConfiguration = new MigrationConf();
Database.SetInitializer<MigratorContext>(
new MigrateDatabaseToLatestVersion<
MigratorContext, MigrationConf>(true, migrationConfiguration));
}
public sealed class MigrationConf : DbMigrationsConfiguration<MigratorContext>
{
public MigrationConf()
: base()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
public CustomContext() : base(DbConnections.GetCurrentConnectionString) { }What this does is your context will still have a parameterless constructor, but the connectionstring will depend on whatever criteria was setup previously (for me, the user selects dev, test, prod)