EF Data migrations won't detect changes when a

2019-02-07 18:01发布

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.

14条回答
叼着烟拽天下
2楼-- · 2019-02-07 18:28

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.

查看更多
Melony?
3楼-- · 2019-02-07 18:31

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.

查看更多
Melony?
4楼-- · 2019-02-07 18:36

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.

查看更多
forever°为你锁心
5楼-- · 2019-02-07 18:40

The problem in my case was caused by:

  1. Create a migration (successfully)
  2. Decide that I want to re-create it, and delete the migration .cs file
  3. Try to regenerate it, and end up with empty migration's Down and Up functions

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

查看更多
戒情不戒烟
6楼-- · 2019-02-07 18:43

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.

Enable-Migrations -ProjectName -StartupProjectName --ConnectionStringName -Force

查看更多
smile是对你的礼貌
7楼-- · 2019-02-07 18:44

I had a problem similar to this, where using the -force flag on add-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. Tried add-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.

查看更多
登录 后发表回答