I am using Entity Framework 5.0 Data migrations along with code first. When i add a new field to my model and execute the following command in the package manager console.
In my case it was because I had added a secondary context 'ApplicationDbContext' as part of the ASP.net identity stuff. When I ran the 'enable-migrations' command again I got an error that there was more than one context. Once I combined the two things started working again.
The batch build -> clean option did not work for me.
I solved the problem by:
The web application now runs successfully, so essentially I had an issue that was fixed by adding meta-data only.
It happened to me and nothing worked. Then i did this on my own and everything works now.
Problem: I created a Model "Cars". And When I create a migration for it using command "add-migartion AddCarModel", a migratoin was created but it was empty. I tried with different names and also tried delete migration's .cs file but nothing worked. Then I did the following:
Solution: Follow below steps:
1. Delete all the empty migrations that you created for the Model. (But remember the names of the migrations for step 2)
2. Also delete those migration entries from "_MigrationHistory" table.
3. Comment out you line(s) of your model DB context, (in my case it is "public DbSet Cars{ get; set; }")
4. Clean and Rebuild solution. (Its best that if you batch clean)
5. Make sure that your update command is working and not throwing errors. (Command: "update-database -verbose")
6. Now uncomment line(s) that you commented in step 3.
7. Now create the migration for that model. (I created the migration with same name as before)
Hopefully it works. :-)
I had a similar problem where a new migration was not being found, and so update-database
was giving me the following error no matter what I did:
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.
Doing a "batch clean" solved my problem, suggesting EF was using an old/invalid assembly from a folder other than the currently selected 'solution configuration (e.g. DEBUG)'.
To do a batch clean:
Main Menu -> Build -> Batch Build...
Select All
Clean
Close dialog, rebuild and re-attempt migration.
Hope this helps someone else out there.
I had to run dotnet ef migrations remove
even though I'd deleted the previous migration
Maybe the stupidest of all:
I was adding a migration with the same name as the new object I was creating.