ASP.NET MVC-如何修复本地和生产服务器上的EF迁移历史记录差异

发布于 2025-02-10 04:52:21 字数 717 浏览 1 评论 0原文

我有一个使用EF迁移的ASP.NET MVC应用程序。通常,当我在本地开发中更改模型时,我会创建新的迁移,然后应用生产更改,我会生成一个带有修改的脚本,并将其应用于生产服务器DB。 我已经这样做了几个月了,显然效果还不错,所以我认为两个数据库都是同步的(即两个数据库中的迁移史表都是相同的)。

但是现在我刚刚意识到__ MigrationHishory的内容表在本地和服务器中有所不同:在Local中,服务器中不存在一个迁移(请注意:它不是最后一个迁移从几个月前)。因此,假设在本地我使用这些迁移(如迁移史表所示):

migration 1
migration 2
migration 3
migration 4

但是在服务器中,我没有迁移2。

我不明白这是怎么发生的:我不允许这样当我尝试在服务器中应用迁移3时,要丢失一个错误,但显然没有...

所以问题是:我现在该如何解决?

我的第一个想法是

  1. 修改本地模型,以删除有问题的迁移
  2. 删除迁移文件的
  3. 更改删除本地数据库
  4. 重新创建本地数据库,并使用更新数据库
  5. 重新完成对模型的更改,
  6. 以创建新的迁移
  7. 更新DB,以应用新的迁移
  8. 脚本,并将其更改应用于更改 如果我对的话,将其应用于生产数据库

,这两个数据库都将同步?还是解决这一切的正确方法是什么?

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

  1. modify local model to remove the changes on the offending migration
  2. delete migration file
  3. delete local database
  4. recreate local database with update-database
  5. re-do changes to the model
  6. create new migration
  7. update db to apply new migration
  8. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文