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

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

问题描述

我使用的是EF 6.0我在C#中使用手动迁移和更新项目。我有数据库上约5迁移,但我意识到,这最后的迁移是坏,我不想要它。我知道,我可以回滚到以前的迁移。但是,当我添加新的(​​固定)的迁移和运行更新的数据库,那么即使是不好的迁移应用。查阅全文。

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 previous migration. But then, when I add new (fixed) migration and run Update-Database, then even the bad migration is aplied.

我试图回滚到以前的迁移和删除不良迁移的文件。但是,当我尝试添加新的迁移,我在更新数据库时,因为迁移文件已损坏出现错误(更特异性地,第一行代码表重命名为B和旁边的线条,EF试图更新表name一个 - 也许这是一些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 specificly, 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像忘记过去的移民喜欢它从来没有存在过,它不好?喜欢的东西删除迁移。

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.

EDIT1
我找到了解决方案,适合我。改变模式以良好的状态和运行添加迁移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.

不管怎样,这还没有完全ANWER原来的问题。如果我UpdateDatabase到坏的迁移,我没有找到好办法如何回滚,并创建新的迁移,不包括坏的。

Anyway, this still not anwer 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.

感谢

推荐答案

您有2个选项:


  • 您可以从坏的迁移下来并把它放在一个新的迁移(你还需要对模型的连续变化)。这有效地卷起到一个更好的版本。

  • 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.

我用的东西,去了多个环境这一选项。

另外一种选择是,以实际运行更新,数据库-TargetMigration:TheLastGoodMigration 对已部署的数据库,然后删除您的解决方案的迁移。这是有点绿巨人粉碎替代,并要求这是对部署了错误版本的数据库中执行

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.

注意:以rescaffold迁移您可以使用添加迁移[existingname] -Force 。然而,这将覆盖现有的迁移,所以一定要做到这一点只有当你已删除从数据库中现有的迁移。这做同样的事情视为删除现有迁移文件并运行添加迁移

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天全站免登陆