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:19

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:

  1. Select Main Menu -> Build -> Batch Build...
  2. Click Select All
  3. Click Clean

Close dialog, rebuild and re-attempt migration.

Hope this helps someone else out there.

查看更多
疯言疯语
3楼-- · 2019-02-07 18:19

Maybe the stupidest of all:

I was adding a migration with the same name as the new object I was creating.

查看更多
Fickle 薄情
4楼-- · 2019-02-07 18:21

I added a new class to my data model to a sub-directory, the resultant namespace was not visible to scaffolding using add-migration.

Fix was to rename the namespace of the new class to conform to the rest of model, and/or add "public virtual DbSet .." etc into your entity context class, which will require you to reference this new namespace, then run add-migration again.

查看更多
Bombasti
5楼-- · 2019-02-07 18:22

Just got the same problem but figured out that my new field was added as a member variable and not a property - it was missing the {get; set;} part and that makes migration skip that field.

May not be your case but it might help someone else.

查看更多
beautiful°
6楼-- · 2019-02-07 18:22

You're 'out of sync' - Db, migrations, code - and you can expect all sorts of problems like that.

I did this million times (almost:) and it works really well - but you need to go steady, and be meticulous with what you're doing.

You can read through this 'summary' I made - start half-way somewhere (but also check connection).

Code first create tables

...and if it doesn't work I'd suggest you make a small 'repeatable' scenario / model - post exactly what you have.

How migrations work:

Migrations are tied to the 'migration table'.

When Add-Migration is run - it checks against the 'existing database' structure and migration table - and makes the 'difference' (sometimes you get none 'up' 'down' simply as too are in sync).

So, each 'migration' is a complex diff in between your code, existing migrations, and database, and migration table. Short of removing the database nothing else is certain to reset - Db 'migration' table may not be enough - that doesn't guarantee full 'cleanup' (if possible, I always do full Db delete). You also need to delete your code migrations.

Make sure to 'compile' the projects (best make them compile automatically in configuration) after/before where relevant.

Make sure your 'connection' matches.

Once all is in sync - all should work nicely - but you gotta keep it in sync. Unless you plan to delete the Db (testing) - don't remove migrations just like that (you can use Update-Database -0 (I think) to get back to some migration (this is 'zero state').

查看更多
啃猪蹄的小仙女
7楼-- · 2019-02-07 18:25

Don't forget to include Public access modifier:

public string Text { get; set; }
查看更多
登录 后发表回答