Entity Framework trying to add migration of tables that are already in database

南楼画角 提交于 2021-02-10 09:14:00

问题


When trying to run my ASP.NET MVC application locally, everything works fine. But once I've deployed it Azure, it keeps giving me the error:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled.

So I connected to the remote database using the Package Manager Console in Visual Studio and ran the Update-Database command. This gave me the same error. When I look at the database inside the SQL Server Management Studio, it looks like all migrations are applied.

So after this I tried running Add-Migration to see if there were any changes I didn't notice. The migration it created was exactly the same as the previous two migration (there is nothing in there that's not in one of those migrations).

So far the site ran just fine for about two weeks, including a few migrations. I'm at a loss as to how to solve this, so any help is appreciated.

I'm certain I'm connecting to the right database, because I see it change when I update it to a target migration. I'm also targeting the correct project.


回答1:


If you are sure that the Add-Migration is creating the script for changes that are already applied, you need to update the migration state of your database. You can do this by adding an empty migration. This will capture the state of your current model.

Add-Migration MergeChanges –IgnoreChanges 

After running this command you will have an empty migration script. Now you can update your database to match the model state. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.

NOTE Run this only if you do not have any pending changes and yet you see the error message to update the database.



来源:https://stackoverflow.com/questions/50355195/entity-framework-trying-to-add-migration-of-tables-that-are-already-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!