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

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

    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.

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

    The batch build -> clean option did not work for me.

    I solved the problem by:

    1. Creating a migration with 'Add-Migration NameOfMigration'
    2. Deleting the contents of the up and down functions of the created migration class.
    3. Updating the database by running the migration script (which will just add a row to the _MigrationHistory table with 'Update-Database -Verbose'

    The web application now runs successfully, so essentially I had an issue that was fixed by adding meta-data only.

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

    It happened to me and nothing worked. Then i did this on my own and everything works now.

    Problem: I created a Model "Cars". And When I create a migration for it using command "add-migartion AddCarModel", a migratoin was created but it was empty. I tried with different names and also tried delete migration's .cs file but nothing worked. Then I did the following:

    Solution: Follow below steps:

    1. Delete all the empty migrations that you created for the Model. (But remember the names of the migrations for step 2)

    2. Also delete those migration entries from "_MigrationHistory" table.

    3. Comment out you line(s) of your model DB context, (in my case it is "public DbSet Cars{ get; set; }")

    4. Clean and Rebuild solution. (Its best that if you batch clean)

    5. Make sure that your update command is working and not throwing errors. (Command: "update-database -verbose")

    6. Now uncomment line(s) that you commented in step 3.

    7. Now create the migration for that model. (I created the migration with same name as before)

    Hopefully it works. :-)

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

    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.

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

    I had to run dotnet ef migrations remove even though I'd deleted the previous migration

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

    Maybe the stupidest of all:

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

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