c# - 具有数据更改的实体框架可恢复迁移?
问题描述
如果您创建一个仅更改表结构的迁移,那么您确信您可以轻松地恢复迁移,因为它具有适用于它Up()
的Down()
方法。
但是,如果我们有一些似乎不可恢复的数据更改怎么办?
例如,现在我有一个可以为空的列,其中包含Enum
值。
Paid column
---
Paid = 0
PaidTill = 1
NotPaid = 2
(+ NULL value)
所以有4个值。现在我想将此列更改为只有 2 个值,并且此列不再可为空:
Paid column
---
NotPaid = 0
Paid = 1
因此,如果该值为 NULL 它将是NotPaid
,PaidTill
成为Paid
等等。
我知道我可以Up()
使用数据操作 SQL 扩展方法,但是这种迁移可以恢复吗?
解决方案
您描述的数据操作显然会丢失数据。除非您可以根据系统中的其他数据扣除原始值,否则无法还原它。
如果您没有其他数据,您可以将其创建为迁移的一部分。备份方法中的原始值Up
(复制到单独的列/表/模式/数据库)。在该Down
方法中,从备份中读取原始值,并删除备份:
protected override void Up(MigrationBuilder migrationBuilder)
{
// before any manipulations:
// create a backup table and copy data from the original column
// assuming MyId is the primary key in the original table
migrationBuilder.Sql(@"
SELECT *
INTO MyNewBackupTable
FROM (SELECT MyId, MyNullableColumn FROM MyOriginalTable)
");
// ... perform the desired manipulations
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// ... revert other manipulations,
// (including making the column nullable again)
// update the column from the data in the backup table
migrationBuilder.Sql(@"
UPDATE MyOriginalTable
SET t1.MyNullableColumn = t2.MyNullableColumn
FROM MyOriginalTable AS t1
INNER JOIN MyNewBackupTable AS t2
ON t1.MyId = t2.MyId
");
// remove the backup table
migrationBuilder.Sql(@"DROP TABLE MyNewBackupTable");
}
Up
如果要求允许您定义“不归路点”,您还可以在后续迁移之一 ( ) 中删除备份。
推荐阅读
- reactjs - Firebase Admin SDK verifySessionCookie 不对 Firebase 存储用户进行身份验证
- list - 如何加载一长串项目并在 React Native 中搜索?
- javascript - 获取我的对象属性的问题
- javascript - 用文本Angular 6替换HTML标签
- apache-flink - Flink:理解我的程序的数据流
- swift - 内容滚动到债券以外的索引路径行?
- c# - 插入具有数据库中存在的一个相关实体的实体
- python - 散景 on_change 补丁
- php - PHP multi_query 页面渲染失败
- android - Android - Firebase 数据库快速入门 Gradle 项目同步失败