Short version:
After deleting the entire database (including __MigrationHistory) AND all the migrations in the solution... somehow, named migrations are being found somewhere and applied by DbMigrator! Where are they coming from???
Long version:
We are using Entity Framework 6, against SQL Server 2012. It's an MVC webapp, and we are using code-based migrations, and we have a moderately long history of migrations at this point. We run migrations at startup:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
}
}
On several PCs it is working fine, but on just one new PC, I have problems where it seems out of synch in some way. Whenever the app runs, it complains that there are pending changes.
System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException occurred
HResult=-2146233088
Message=Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()
at CVInsights.MvcApplication.Application_Start() in c:\Work\ClearVision\CVO\CVInsights\Global.asax.cs:line 20
InnerException:
But running add-migration shows no changes, and update-database does nothing.
So, giving up for the moment, I wanted to "reset" EF migrations fully. So I deleted the entire database (obviously including the __MigrationHistory table). And I deleted all the migrations from the IDE Migrations folder.
Now... when I start our app on this new PC... the DbMigrator still finds and applies a bunch of named migrations from somewhere! It isn't the complete list we presently have (in our source control), but it is the list up to a certain point a few weeks ago. After applying some of these "zombie migrations", it throws the same old exception "
Where the heck are they coming from??
I feel like I must be doing something very stupid, or the PC is fundamentally messed up...
Configuration.cs:
public sealed class Configuration : DbMigrationsConfiguration<CloudDatabase.DAL.DatabaseContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(CloudDatabase.DAL.DatabaseContext context)
{
context.Roles.AddOrUpdate(role => role.ID, // This is the primary key or the table we are adding to or updating.
new Role() { ID = 1, Name = "A role" },
new Role() { ID = 2, Name = "another role" },
new Role() { ID = 3, Name = "third role" }
);
}
}
Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.
Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project. json file. Alternatively, run "Remove-Migration" command in the package manager console.
Based on the comments chain and what you seem to have tried, I would suggest the following course of action. Please note I am assuming you have a code store of some form (if not please backup your code).
update-database
command and run your project. If that does not fix your project then my guess would be its a problem with the Visual Studio installation (reinstall, I hope not).
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With