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

后端 未结 16 1852
情话喂你
情话喂你 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:15

    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.

    0 讨论(0)
  • 2020-12-09 15:16

    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.

    0 讨论(0)
  • 2020-12-09 15:17

    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.

    0 讨论(0)
  • 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').

    0 讨论(0)
  • 2020-12-09 15:19

    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

    0 讨论(0)
  • 2020-12-09 15:21

    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.

    0 讨论(0)
提交回复
热议问题