I'm fairly new to Entity Framework and I am having trouble getting it to apply my code first migrations to a new database. I think my migrations have gotten into a bad state.
I started out with an existing database, so I created an initial empty migration using
Add-Migration Initial -IgnoreChanges
That worked fine. Then I added a new entity and created another migration
Add-Migration New_Entity
That worked fine. I could apply these migrations to an existing database that didn't even have a __MigrationHistory
table and it would be properly updated.
Fast forward a few days and a few commits later. I now have a new "clean" database that I want to apply the migrations to. I got into Visual Studio, point my app.config file at the new database, run
Update-Database
And it tells me
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. You can use the Add-Migration command to write the pending model changes to a code-based migration.
However, I do not have any pending models changes. Nothing has changed. But just to see what would happen, I ran
Add-Migration x
and it created a new migration that would add every entity in the model to the database. Basically starting over as if I had no migrations. So I deleted that one then re-added with the -IgnoreChanges
flag. That gives me another blank one. If then run
Update-Database
It runs the x
migration but totally ignores the migrations I created a few days ago (Initial
and New_Entity
). It's like it doesn't know they exist.
Obviously something is in a bad state but I'm not sure what I can do to get it back. I can't just delete and recreate the migrations because I need a blank one and one with just the new entity. I can't just comment out the entity temporarily because a lot of code already depends on it.
How did I get myself into this mess and how do I get out?