Bok*_*kos 3 c# sql-server entity-framework ef-core-2.2
在这样的子实体上设置外键时:
modelBuilder.Entity<Child>()
.HasOne(c => c.Parent)
.WithMany(p => p.Children)
.HasForeignKey(c => c.ParentId)
.OnDelete(DeleteBehavior.Cascade);
Run Code Online (Sandbox Code Playgroud)
我能够配置约束的“ON DELETE”行为。由此产生的迁移看起来像:
migrationBuilder.CreateTable(
name: "Child",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
ParentId = table.Column<Guid>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Child", x => x.Id);
table.ForeignKey(
name: "FK_Child_Parent_ParentId",
column: x => x.ParentId,
principalTable: "Parent",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
Run Code Online (Sandbox Code Playgroud)
在生成的迁移中,我可以直接编辑 onUpdate 行为,如方法签名中所示:
public virtual OperationBuilder<AddForeignKeyOperation> ForeignKey(
[NotNull] string name,
[NotNull] Expression<Func<TColumns, object>> column,
[NotNull] string principalTable,
[NotNull] string principalColumn,
[CanBeNull] string principalSchema = null,
ReferentialAction onUpdate = ReferentialAction.NoAction,
ReferentialAction onDelete = ReferentialAction.NoAction);
Run Code Online (Sandbox Code Playgroud)
但我无法通过 fluent API 或属性找到一种方法来做到这一点。
是否可以为外键配置 EF Core“ON UPDATE”行为?