EntityFramework transitions become useless after moving to a new context. DbMigrator uses a list of pending migrations from the first database instance, which means that no migrations are applied to other databases, which then leads to errors during Seed ();
- C # .NET 4.5 MVC Project with EF 6
- MS SQL Server 2014, multiple instances of the same database model.
- CodeFirst with migrations.
- The DbContext initializer is null.
At the beginning of the application, we have custom Db initialization for creating and updating databases. CreateDatabaseIfNotExists
works as intended, the new databases apply all migrations. However, both the MigrateDatabaseToLatestVersion
initializer and our user initializer cannot update databases other than the first in the list.
foreach (var connectionString in connectionStrings) { using (var context = new ApplicationDbContext(connectionString)) {
context.Database.CreateIfNotExists();
works correctly.migrator.GetLocalMigrations()
always returns the correct values.migrator.GetPendingMigrations()
after returning the first database an empty list.migrator.GetDatabaseMigrations()
is a mirror of pending migrations; after the first database, it contains the full list event for empty databases.- Getting data (
context.xxx.ToList()
) from a Db instance confirms that the connection is up and running, and links to the correct instance.
Force update the latest migration using migrator.Update("migration_name");
nothing changes. From what I collect by reading the source code for EF, it checks the list of pending migration itself, which gives it erroneous results.
It seems that caching happens under the hood, but it eludes me like reset.
Is there a way to perform migrations in multiple databases or is this another “design error” in EF?
Edit:
The Real Problem DbMigrator
creates a new Context for its own use. He does this with the constructor without parameters without parameters, which in my case had a rejection of the default connection string (first) in web.Config
.
I don't see a good solution to this problem, but in my case, a primitive workaround is to temporarily change the default connection string:
var originalConStr = WebConfigurationManager.ConnectionStrings["ApplicationDbContext"].ConnectionString; var setting = WebConfigurationManager.ConnectionStrings["ApplicationDbContext"]; var fi = typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
Cheat from: How to configure connection string programmatically in .net?
I still hope that someone will find a real solution, so for now I will refrain from self-deception.