EF migration shows empty Up() Down() methods

后端 未结 17 2211
花落未央
花落未央 2020-12-25 09:56

I have a local database that is currently in it\'s second version and should now go to it\'s third version.

The code for the previous migrations was generated by ano

相关标签:
17条回答
  • 2020-12-25 10:08

    To me the problem was that Id property that should correspond to table id was named FeedbackId. I changed to "Id" and then Up/Down weren't empty anymore. Dunno if that can help somehow

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

    I was able to fix this issue by deleting a record of last migration from _MigrationHistory table. This record had been incorrectly created before I added DbSet for new model object to DbContext class. After this deletion new migration was created with correct Up() and Down() methods.

    0 讨论(0)
  • 2020-12-25 10:10

    if new tables added to Context just remove new table in "Migration/ExampleContextModelSnapshot"

    0 讨论(0)
  • 2020-12-25 10:11

    In my case, the datacontext project is a class lib project. It is different from the startup project which is asp.net mvc 5 project. Now by mistake the connection string in the startup project is pointing to a different database.

    So ensure that datacontext project and startup project point to the same database. Also use the full command as mentioned in the question like the following. You can include -Force as well.

    add-migration "InitialMigration" -verbose -ProjectName "MyEFproject" -Force
    
    0 讨论(0)
  • 2020-12-25 10:13

    I had to Update-Database with the latest migration before the empty one appending this parameter -TargetMigration:"{your-migration-name}".

    Probably it will tell you that there will be data loss from the next buggy one we tried. If you can afford it append -Force to it.

    Then I tried to add my new Add-Migration and it wasn't empty.

    Final thing that you may need to do if above is throwing exception is to go SQL Server Management Studio and delete the last Automatic migration and try to add it again.

    0 讨论(0)
  • 2020-12-25 10:13

    In my case, I was encountering similar problems with Visual Studio Code.

    I have fixed these by doing the following:

    1. Check inside your ContextModelSnapshot : ModelSnapshot
    2. Comment Model Entity Definition…
    3. Delete your migration files related to these entity
    4. Delete the migrations from the dbo.__EFMigrationsHistory table
    5. Compile your solution.
    6. Run the following commands:
    dotnet ef migrations add migrationName -p ProjectContainer/ 
    dotnet watch run 
    
    0 讨论(0)
提交回复
热议问题