Entity Framework Core:在更改模型/实体后避免数据丢失的脚手架 [英] Entity Framework Core: Scaffolding to Avoid Data Loss after to change Model/Entity

查看:17
本文介绍了Entity Framework Core:在更改模型/实体后避免数据丢失的脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

public class Promotion : BaseModel
{
    [Required]
    public string Description { get; set; }

    [Required]
    public string Market { get; set; }

    [Required]
    public double Price { get; set; }
}

它创建了一个更新到数据库的迁移.并且在数据库中插入了一些促销.但我需要将促销模型更改为:

It was created a Migration that was updated to the database. And some promotions was inserted on the database. But I needed to change the Promotion Model to this:

public class Promotion : BaseModel
{
    [Required]
    public string Description { get; set; }

    [Required]
    public Market Market { get; set; }

    [Required]
    public double Price { get; set; }
}

public class Market : BaseModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public string Adress { get; set; }
}

当我添加一个新的迁移时,我收到警报:一个操作是脚手架,可能会导致数据丢失.请检查迁移的准确性."

When I added a new Migration, I got the alert: "An operation was scaffolding that may result in the loss of data. Please review the migrations for accuracy."

这是新迁移的自动生成的 Up 方法:

That's the auto generated Up method of the new Migration:

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "Market",
            table: "Promotions");

        migrationBuilder.AddColumn<Guid>(
            name: "MarketId",
            table: "Promotions",
            nullable: false,
            defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

        migrationBuilder.CreateTable(
            name: "Markets",
            columns: table => new
            {
                Id = table.Column<Guid>(nullable: false),
                Adress = table.Column<string>(nullable: false),
                CreatedAt = table.Column<DateTime>(nullable: false),
                DeletedAt = table.Column<DateTime>(nullable: false),
                Name = table.Column<string>(nullable: false),
                UpdatedAt = table.Column<DateTime>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Markets", x => x.Id);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Promotions_MarketId",
            table: "Promotions",
            column: "MarketId");

        migrationBuilder.AddForeignKey(
            name: "FK_Promotions_Markets_MarketId",
            table: "Promotions",
            column: "MarketId",
            principalTable: "Markets",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    }

如何在不丢失数据的情况下更新数据库?

How can I update the database without lose data?

推荐答案

升级生产 DB 的安全方法是将其分解为多个步骤:

The safe way to upgrade production DB is to break it up in multiple steps:

  1. 添加新的Market实体并将其附加到Promotion实体不删除现有列
  2. EF 将为您生成迁移 - CREATE TABLE + ADD FOREIGN KEY 语句
  3. 使您的代码从 Market tableMarket column 中更新/插入/选择新值,更喜欢 Market table
  4. 您部署它.现在,您已经获得了包含数据的旧列和新表,并同步了新数据.
  5. 编写数据迁移,将旧值从 Market column 复制到 Market table.运行.现在,您已将数据移至新的 Market table,新数据位于 Market table
  6. 更新您的代码以停止使用旧的Market column.部署更改
  7. 从您的实体中删除Market column.EF 将生成迁移,其中列将被删除.部署这个.您现在已迁移数据和架构
  1. Add new Market entity and attach it to Promotion entity without dropping the existing column
  2. EF will generate you a migration - CREATE TABLE + ADD FOREIGN KEY statements
  3. Make your code to update/insert/select new values from both Market table and Market column preferring Market table
  4. You deploy this. Now, you've got both old column with data and new table out there, being in sync for the new data.
  5. Write a data migration, which will copy old values from Market column to Market table. Run it. Now you've got your data moved to the new Market table and new data sitting in Market table
  6. Update your code to stop using old Market column. Deploy the change
  7. Remove Market column from you entity. EF will generate migration where column will be dropped. Deploy this. You now have your data and schema migrated

这篇关于Entity Framework Core:在更改模型/实体后避免数据丢失的脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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