Sér*_*eno 2 scaffolding entity-framework-core asp.net-core
我有以下模型:
public class Promotion : BaseModel
{
[Required]
public string Description { get; set; }
[Required]
public string Market { get; set; }
[Required]
public double Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它已创建一个已更新到数据库的迁移。并且一些促销已插入数据库中。但是我需要将促销模型更改为:
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; }
}
Run Code Online (Sandbox Code Playgroud)
当我添加一个新的迁移时,我得到了警报:“某个操作正在脚手架上,可能会导致数据丢失。请检查迁移的准确性。”
这是新迁移自动生成的Up方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
如何在不丢失数据的情况下更新数据库?
升级生产数据库的安全方法是分多个步骤进行分解:
Market
实体并将其附加到Promotion
实体without dropping the existing column
CREATE TABLE
+ ADD FOREIGN KEY
声明Market table
并且Market column
更喜欢Market table
Market column
到Market table
。运行。现在,您已将数据移至Market table
新数据中Market table
Market column
。部署变更Market column
从您的实体中删除。EF将在列将被删除的位置生成迁移。部署这个。现在,您已迁移了数据和架构 归档时间: |
|
查看次数: |
3181 次 |
最近记录: |