简体   繁体   English

FluentMigration备份表

[英]FluentMigration Backup Table

I have a Table which needs removal of a few columns. 我有一个需要删除一些列的表。

TableA 
{
  ID int, 
  Name NVarChar(MAX) NOT NULL, 
  ToDelete1 int NOT NULL,
  ToDelete2 int NOT NULL,
}

I'm using FluentMigrator .NET (ActiveRecord Ruby On Rails like Migration). 我正在使用FluentMigrator .NET(像Migration一样的ActiveRecord Ruby On Rails)。 For the UP() migration it's a simple case of dropping the columns. 对于UP()迁移,这是删除列的简单情况。

How do I create the down part of the migration? 如何创建迁移的向下部分?

My idea is to create a clone of the table during the UP() process and then copy the schema and data back to the Original during the Down() process. 我的想法是在UP()过程中创建表的克隆,然后在Down()过程中将模式和数据复制回原始。

What is the easiest way to achieve this? 最简单的方法是什么?

EDIT: TableA.ID is referenced by other tables. 编辑: TableA.ID被其他表引用。 So if we are deleting all rows from TableA and reinserting from TableA_Backup it has to be done while all foreign key constraints are turned off and with identity_insert turned on as far as I understand. 因此,如果我们要删除TableA中的所有行,并从TableA_Backup中重新插入,则必须在所有外键约束都已关闭且Identity_insert打开的情况下进行。

A code example would be great. 一个代码示例会很棒。

For anyone who is interested here is how I managed to get it to work. 对于任何对这里感兴趣的人,我都是如何设法使其正常工作的。

  1. Renamed the Original table with a _Backup prefix. 用_Backup前缀重命名了原始表。
  2. Copied items from that table to a new table schema with the Original name. 该表中的项目已复制到具有原始名称的新表架构中。 With Identity Insert. 与身份插入。
  3. Removed foreign keys referring to old table. 删除了引用旧表的外键。
  4. Added foreign keys back to refer to new table. 添加了外键以引用新表。

Each of these steps are reversible and you can create the down migration. 每个步骤都是可逆的,您可以创建向下迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM