EF Data migrations won't detect changes when adding new migration

后端 未结 16 1935
情话喂你
情话喂你 2020-12-09 14:53

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.

16条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 15:19

    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').

提交回复
热议问题