从EF6中的__migrationhistory表中删除数据后,如何在不丢失任何数据的情况下进行迁移? [英] How do I take migration without losing any data after deleting data from __migrationhistory table in EF6?

查看:538
本文介绍了从EF6中的__migrationhistory表中删除数据后,如何在不丢失任何数据的情况下进行迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心从__MigrationHistory表中删除了数据,并尝试将新表和列更改迁移到db。这时EF6给我一个错误



数据库中已经有一个名为'xxxx'的对象。。

我尝试过使用此命令的方式,对表列更改没有影响。



I have accidentally deleted data from __MigrationHistory table and I tried to make migration new tables and column changes to db. At this time EF6 show me an error

"There is already an object named 'xxxx' in the database.".
I tried the way by using this command and there is no effect on table column changes.

Add-Migration MyMigration1 -IgnoreChanges
Update-database -Force



有很多关于重建db或其他的建议。但这些方式可能会影响数据丢失到我的数据库,其中包含大量的测试数据,我无法丢失任何数据。如何在不丢失任何数据的情况下解决此错误。如果有人请...



我尝试过:



我尝试使用此命令,并且对表列更改没有影响。




There are many recommendation about this for recreating db or another else. But those ways might be effect data losing to my db which contains a huge size of tested data and I can't able to lost any data. How to solve this error by without losing any data. If anybody please...

What I have tried:

I tried the way by using this command and there is no effect on table column changes.

Add-Migration MyMigration1 -IgnoreChanges
Update-database -Force

推荐答案

运行 Update-Database -Script -SourceMigration:0 为你的数据库生成一个SQL脚本。



然后你需要通过脚本来查找 CREATE TABLE INSERT _MigrationHistory 表的语句,并针对您的数据库运行这些语句。 注意:如果您只删除了数据,并且 _MigrationHistory 表仍然存在,那么您只需要 INSERT 语句。



如果您的所有数据库都已经是最新的,您还可以尝试Rick Strahl描述的方法:

将实体框架迁移重置为干净的Slate - Rick Strahl的Web日志 [ ^ ]

但要注意警告:

Run Update-Database -Script -SourceMigration:0 to generate a SQL script for your database.

You'll then need to pick through the script to find the CREATE TABLE and INSERT statements for the _MigrationHistory table, and run just those statements against your database. NB: If you've only deleted the data, and the _MigrationHistory table still exists, you'll only need the INSERT statements.

If all of your databases are already up-to-date, you could also try the approach described by Rick Strahl:
Resetting Entity Framework Migrations to a clean Slate - Rick Strahl's Web Log[^]
But pay attention to the warning:
Quote:

一旦应用了更新,您的迁移起始点就完全没有数据库,或者处于完全更新的基本状态的数据库。如果在开始清理平板操作之前,您的数据库在迁移中有几次迭代,那么就没有简单的方法可以使这些数据库同步。

Once the updates are applied your migration starting points are either no database at all, or the database in the fully updated base state. If you have databases that were a few iterations behind in migrations before you started the clean slate operations, there will be no easy way to get those in sync.





一旦解决了问题,请备份数据库并设置预定作业以使其备份。下次遇到这样的问题时,您只需从上一次良好的备份恢复即可。



Once you've fixed the problem, back up your database and set up a scheduled job to keep it backed up. Next time you have a problem like this, you will simply be able to restore from the last good backup.


这篇关于从EF6中的__migrationhistory表中删除数据后,如何在不丢失任何数据的情况下进行迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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