Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迁移脚本创建外键_C#_Entity Framework - Fatal编程技术网

C# 迁移脚本创建外键

C# 迁移脚本创建外键,c#,entity-framework,C#,Entity Framework,我创建了一个迁移脚本来在表中添加外键 migrationBuilder.AddColumn<int>( name: "LinkId", table: "a", nullable: false); migrationBuilder.CreateIndex( name: "IX_a_LinkId", ta

我创建了一个迁移脚本来在表中添加外键

migrationBuilder.AddColumn<int>(
                name: "LinkId",
                table: "a",
                nullable: false);

            migrationBuilder.CreateIndex(
                name: "IX_a_LinkId",
                table: "a",
                column: "LinkId");

            migrationBuilder.AddForeignKey(
                name: "FK_a_b_LinkId",
                table: "a",
                column: "LinkId",
                principalTable: "b",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
migrationBuilder.AddColumn(
名称:“LinkId”,
表:“a”,
可为空:false);
migrationBuilder.CreateIndex(
名称:“IX_a_LinkId”,
表:“a”,
列:“LinkId”);
migrationBuilder.AddForeignKey(
名称:“FK_a_b_LinkId”,
表:“a”,
列:“LinkId”,
原则性:“b”,
主栏:“Id”,
onDelete:referentialiction.Restrict);

这有助于我创建外键,但我在显示为null的列中没有看到任何值。是否需要自动获取值,还是必须使用更新脚本手动更新外键列中的值?

必须在迁移类中添加更新查询

public分部类LinkIdColumn\u v1:DbMigration
{
公共覆盖作废()
{
AddColumn(“a”,“LinkId”,c=>c.Int());
Sql(“在a.SomeKey=b.SomeKey上从b内部连接a更新集a.LinkId=b.LinkId”);
//创建FK的其他生成代码
}
公共覆盖无效向下()
{
//生成用于删除FK的代码
}
}

您必须在迁移类中添加更新查询

public分部类LinkIdColumn\u v1:DbMigration
{
公共覆盖作废()
{
AddColumn(“a”,“LinkId”,c=>c.Int());
Sql(“在a.SomeKey=b.SomeKey上从b内部连接a更新集a.LinkId=b.LinkId”);
//创建FK的其他生成代码
}
公共覆盖无效向下()
{
//生成用于删除FK的代码
}
}