7

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" } ); } } 
5
  • Are you sure you deleted all migrations (perhaps some are in different folder)? It sounds to me like it's applying part of your migrations, then tries to apply the rest of the changes in your context as an automatic migration. Do you have a "baseline" database or are your migrations designed to start with a blank database? Commented Jun 12, 2015 at 0:54
  • Hi Vesan. I think I deleted all migrations. I deleted all the datetime_name.cs files in the Migrations folder. There are no subfolders. There is still a configuration.cs with not much in it. The migrations start with a blank database - as I said I delete the entire database to start. Automatic migrations are turned off. ( AutomaticMigrationsEnabled = false; in configuration.cs) Commented Jun 12, 2015 at 1:30
  • Hmm it sounds like you covered pretty much everything. Have you checked the actual dll that's running with ildasm/reflector to make sure it's the right one and not an old build or something? Maybe you can also try migration logging if it doesn't tell you something useful (see here). Otherwise I have no more ideas. Hopefully someone will be able to help. Commented Jun 12, 2015 at 1:37
  • I've added configuration.cs. Commented Jun 12, 2015 at 1:44
  • Yout might try looking at this or this question to see if they don't help. Commented Jun 12, 2015 at 1:59

1 Answer 1

5

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).

  1. Drop the database in question.
  2. Close all copies of Visual Studio.
  3. Delete the source from your solution folder (all of it, leave nothing behind).
  4. Extract a copy of the code from your code store (Pull, Get etc)
  5. Rebuild the solution
  6. Run the 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.

Sign up to request clarification or add additional context in comments.

3 Comments

This seems to have solved the problem. The migration history is "somewhere" in my working folders, but I don't know where. Thanks a lot!
Closing and reopening did it for me.
Confirmed, this solved the same problem I was getting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.