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.
"Add-migration AddedField"
All I get is an empty migration called "n_AddedField", the up and down methods contain no logic.
I tried a bunch of things, reinstalling the EF nuget package, cleaning my solution, rebuilding, manually removing all generated files and directories.
Then i decided that i would scrap all my migrations and start over, and then it got weird. After deleting all my migrations, and the migrationhistory table in the database, i recreated the database using the CreateDatabaseIfNotExists initializer. After doing this, I should be able to create a new initial migration. But when i try to create create a new migration, I get an error saying that there are pending migrations, and lists all the migrations that i just deleted from my project.
I have no idea why and how EF still has any recollection of those migrations. I even tried searching through filecontents looking if the migrations were saved somewhere else or something. But nothing..
Data migrations look really neat when scott hansleman demo's it on stage, but for real work, I'm starting to look for alternatives.
When the project started, we were using EF 4.x and a while back switcted to 5.0, but since the switch i have added a bunch of migrations successfully.
Does anyone have any idea how to solve this problem? Basically i just want to be able to add migrations, and generate a sql script with the changes.
oops. In my case I was adding a new root entity not referenced by any other entity. The result was simply that code first had no reason to generate a migration for the entity. Once I added the code into the DbContext (a dbset) it worked like a charm.
I had to delete the _MigrationHistory table that is generated by EF. Then I ran add-migration again. Be careful with this though, as it will generate the queries needed from scratch, including tables that are already there.
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 problem in my case was caused by:
.cs
fileDown
andUp
functionsIn this case, I forgot to also delete the
ApplicationDbContextModelSnapshot.cs
entries for the model changes. Removing the new mappings in this file solved my problem and it then generated correctly.I had the same problem. Migrations were enabled but they weren't detecting any changes. My solution was to re-enable migrations using -Force attribute and then everything worked.
I had a problem similar to this, where using the
-force
flag onadd-migration
to re-scaffold an existing migration stopped working for no apparent reason.No matter what I did I got the stupid "Unable to generate an explicit migration because the following explicit migrations are pending" error message. After trying nearly everything I could think of and stopping just short of smashing my laptop to pieces, out of desperation I ran
enable-migrations
again and of course got the "Migrations have already been enabled in project 'Blah.Blah'" message. Triedadd-migration -force
again and magically it was working.I have no idea what it changed- must have been some user settings/config file outside of source control. Hopefully this will help someone else.