实体框架尝试添加数据库中已经存在的表的迁移 [英] Entity Framework trying to add migration of tables that are already in database

查看:55
本文介绍了实体框架尝试添加数据库中已经存在的表的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试在本地运行ASP.NET MVC应用程序时,一切正常.但是,一旦我将其部署到Azure,它就会不断给我错误:

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.

因此,我使用Visual Studio中的程序包管理器控制台"连接到了远程数据库,并运行了Update-Database命令.这给了我同样的错误.当我查看SQL Server Management Studio中的数据库时,似乎所有的迁移都已应用.

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.

因此,在此之后,我尝试运行Add-Migration以查看是否有任何未发现的更改.它创建的迁移与之前的两次迁移完全相同(其中没有一个是没有迁移的.)

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.

推荐答案

如果您确定 Add-Migration 正在为已应用的更改创建脚本,则需要更新迁移数据库的状态.您可以通过添加一个空迁移来做到这一点.这将捕获您当前模型的状态.

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 

运行此命令后,将有一个空的迁移脚本.现在,您可以更新数据库以匹配模型状态.由于实际的迁移不包含任何更改,因此只需在__MigrationsHistory表中添加一行,以表明该迁移已被应用.

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.

这篇关于实体框架尝试添加数据库中已经存在的表的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆