实体框架回滚并移除不良迁移 [英] Entity Framework rollback and remove bad migration

查看:21
本文介绍了实体框架回滚并移除不良迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中的项目中使用 EF 6.0,并进行手动迁移和更新.我在数据库上进行了大约 5 次迁移,但我意识到最后一次迁移很糟糕,我不想要它.我知道我可以回滚到以前的迁移,但是当我添加一个新的(固定的)迁移并运行 Update-Database 时,即使是错误的迁移也会被应用.

I'm using EF 6.0 for my project in C# with manual migrations and updates. I have about 5 migrations on the database, but I realised that the last migration was bad and I don't want it. I know that I can rollback to a previous migration, but when I add a new (fixed) migration and run Update-Database, even the bad migration is applied.

我试图回滚到之前的迁移并删除迁移错误的文件.但是,当我尝试添加新迁移时,更新数据库时出现错误,因为迁移文件已损坏(更具体地说,第一行代码将表 A 重命名为 B 并且是下一行,EF 正在尝试更新表名称 A - 可能是一些 EF 错误).

I was trying to rollback to the previous migration and delete the file with bad migration. But then, when I try to add new migration, I get error when updating database, because the migration file is corrupted (more specifically, first line of code rename the table A to B and is next lines, EF is trying to update table with name A - maybe it is some EF bug).

是否有一些我可以运行的查询,它会告诉 EF 类似忘记上次迁移,就像它从未存在过一样,这很糟糕"?类似于 Remove-Migration.

Is there some query I can run, which would tell EF something like "Forget last migration like it never existed, it was bad"? Something like Remove-Migration.

编辑1我找到了适合我的解决方案.将模型更改为良好状态并运行 Add-Migration TheBadMigration -Force.这将重新构建最后一个未应用的迁移.

Edit1 I found solution suited for me. Changing model to the good state and run Add-Migration TheBadMigration -Force. This will re-scaffold the last, not applied migration.

无论如何,这仍然不能完全回答原始问题.如果我将 UpdateDatabase 迁移到错误的迁移,我没有找到如何回滚和创建新迁移的好方法,不包括错误的迁移.

Anyway, this still not answer the original question completely. If I UpdateDatabase to the bad migration, I did not found good way how to rollback and create new migration, excluding the bad one.

谢谢

推荐答案

你有两个选择:

  • 您可以从错误的迁移中取出 Down 并将其放入新的迁移中(您还需要对模型进行后续更改).这有效地升级到更好的版本.

  • You can take the Down from the bad migration and put it in a new migration (you will also need to make the subsequent changes to the model). This is effectively rolling up to a better version.

我对已经进入多个环境的东西使用此选项.

另一个选项是对您部署的数据库实际运行 Update-Database –TargetMigration: TheLastGoodMigration,然后从您的解决方案中删除迁移.这有点像 hulk 粉碎替代方案,需要针对使用错误版本部署的任何数据库执行此操作.

The other option is to actually run Update-Database –TargetMigration: TheLastGoodMigration against your deployed database and then delete the migration from your solution. This is kinda the hulk smash alternative and requires this to be performed against any database deployed with the bad version.

注意:要重新构建迁移,您可以使用 Add-Migration [existingname] -Force.但是,这将覆盖您现有的迁移,因此请务必仅在您已从数据库中删除现有迁移时执行此操作.这与删除现有迁移文件并运行 add-migration

Note: to rescaffold the migration you can use Add-Migration [existingname] -Force. This will however overwrite your existing migration, so be sure to do this only if you have removed the existing migration from the database. This does the same thing as deleting the existing migration file and running add-migration

我在开发时使用此选项.

这篇关于实体框架回滚并移除不良迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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