【发布时间】:2020-01-20 12:19:34
【问题描述】:
我正在尝试使用实体框架进行第二次迁移,到目前为止我生成了这样的代码
namespace Entity.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class LedgerDisplayOrder : DbMigration
{
public override void Up()
{
AddColumn("Ledgers", "DisplayOrder", c => c.Int());
// Hic sunt leones
}
public override void Down()
{
DropColumn("Ledgers", "DisplayOrder");
}
}
}
如何填充这一列的逻辑其实很简单,在 SQL 中应该是这样的:
ALTER TABLE [Ledgers] ADD [DisplayOrder] INT NULL;
UPDATE [Ledgers]
SET DisplayOrder = 10
WHERE [Id] = 26 OR [Id] = 27 OR [Id] = 23;
UPDATE [Ledgers]
SET DisplayOrder = 20
WHERE [Id] = 29 OR [Id] = 9;
UPDATE [Ledgers]
SET DisplayOrder = 30
WHERE [Id] = 28 OR [Id] = 23;
换句话说,迁移应该在迁移过程中自动填充,但我不确定在// hic sunt leones 放置什么来实现这一点。我读到我应该使用migrationBuilder class,可能类似于
migrationBuilder.InsertData(table: "ledger", column: "DisplayOrder", value: "10");
但是如何实现WHERE 子句呢?以及如何加载该类?我的 Visual Studio 的 Quick Actions and Refactoring 一直建议实现一个新类,所以我必须先安装一些东西吗?
感谢任何帮助!
参考:到目前为止,我发现的只是为新列设置默认值。这不是我所追求的。
【问题讨论】:
标签: c# asp.net entity-framework entity-framework-migrations code-first