I recently needed to do this, my solution was to create a custom Configuration class. The full code base works perfectly for me.
public class Configuration : DbMigrationsConfiguration<Context>
{
public void Init()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
public Configuration()
{
Init();
}
protected override void Seed(Context context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
public static void UpdateDatabase()
{
try
{
var config = new Configuration();
UpdateDatabase(config);
}
catch (Exception ex)
{
logger.writeLog("UpdateDatabase", ex);
}
}
public Configuration(DbConnectionInfo db)
{
Init();
TargetDatabase = db;
}
public static void UpdateDatabase(DbConnectionInfo db)
{
try
{
var config = new Configuration(db);
UpdateDatabase(config);
}
catch (Exception ex)
{
logger.writeLog("UpdateDatabase", ex);
}
}
private static void UpdateDatabase(Configuration config)
{
var migrator = new DbMigrator(config);
var pending = migrator.GetPendingMigrations();
if (pending.Count() > 0)
migrator.Update();
}
}
}
This offers both a default that uses the connection string specified in the DataContext as well as option to pass the connection.
Default Call
Configuration.UpdateDatabase();
Override with passing connection info
var offlineCon = new System.Data.Entity.Infrastructure.DbConnectionInfo("connection string","provider");
Configuration.UpdateDatabase(offlineCon);
where connection string is your specific connection string and provider the data provider e.g. System.Data.SqlClient
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext)It seems that there is no way to just use the class, I need to create a concrete implementation.