ASP.NET MVC-如何修复本地和生产服务器上的EF迁移历史记录差异
我有一个使用EF迁移的ASP.NET MVC应用程序。通常,当我在本地开发中更改模型时,我会创建新的迁移,然后应用生产更改,我会生成一个带有修改的脚本,并将其应用于生产服务器DB。 我已经这样做了几个月了,显然效果还不错,所以我认为两个数据库都是同步的(即两个数据库中的迁移史表都是相同的)。
但是现在我刚刚意识到__ MigrationHishory的内容
表在本地和服务器中有所不同:在Local中,服务器中不存在一个迁移(请注意:它不是最后一个迁移从几个月前)。因此,假设在本地我使用这些迁移(如迁移史表所示):
migration 1
migration 2
migration 3
migration 4
但是在服务器中,我没有迁移2。
我不明白这是怎么发生的:我不允许这样当我尝试在服务器中应用迁移3时,要丢失一个错误,但显然没有...
所以问题是:我现在该如何解决?
我的第一个想法是
- 修改本地模型,以删除有问题的迁移
- 删除迁移文件的
- 更改删除本地数据库
- 重新创建本地数据库,并使用更新数据库
- 重新完成对模型的更改,
- 以创建新的迁移
- 更新DB,以应用新的迁移
- 脚本,并将其更改应用于更改 如果我对的话,将其应用于生产数据库
,这两个数据库都将同步?还是解决这一切的正确方法是什么?
I have an ASP.NET MVC application that uses EF migrations. Usually when I make changes to the model in local development, I create new migration, and then to apply the changes in production I generate a script with the modifications and apply it to the production server db.
I have been doing this for months, and apparently it worked ok, so I assumed that both databases were synced (i.e. the migrationHistory table was the same in both databases).
But now I have just realised that the contents of the __MigrationHistory
table are different in local and server: in local there is one migration that does not exist in server (note: its not the last migration, its one from months ago). So let's say in local I have these migrations applied (as shown in migrationHistory table):
migration 1
migration 2
migration 3
migration 4
but in server, I don't have migration 2.
I don't understand how this happened: I though EF would not allow this and it should have throw an error when I tried to apply migration 3 in server, but apparently it did not...
So the question is: how can I fix this now?
My first idea is to
- modify local model to remove the changes on the offending migration
- delete migration file
- delete local database
- recreate local database with update-database
- re-do changes to the model
- create new migration
- update db to apply new migration
- generate script with the changes and apply it to the production db
If I am right, with this both databases will be synced? Or what would be the correct way to fix all this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论