【发布时间】:2018-07-09 08:51:37
【问题描述】:
我正在运行 efcore 2.0.1。
我有一个模型:
public class BigAwesomeDinosaurWithTeeth
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public ICollection<YummyPunyPrey> YummyPunyPrey { get; set; }
}
public class YummyPunyPrey
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid? BigAwesomeDinosaurWithTeethId { get; set; }
[ForeignKey("BigAwesomeDinosaurWithTeethId")]
public BigAwesomeDinosaurWithTeeth BigAwesomeDinosaurWithTeeth { get; set; }
}
我对这两个类没有流利的 api。但是当我生成迁移时
constraints: table =>
{
table.PrimaryKey("PK_YummyPunyPrey", x => x.Id);
table.ForeignKey(
name: "FK_YummyPunyPrey_BigAwesomeDinosaurWithTeeth_BigAwesomeDinosaurWithTeethId",
column: x => x.BigAwesomeDinosaurWithTeethId,
principalTable: "BigAwesomeDinosaurWithTeeth",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
当文档说它应该将其作为 ClientSetNull
处理时,为什么会生成 onDelete: ReferentialAction.Restricthttps://docs.microsoft.com/en-us/ef/core/saving/cascade-delete
行为名称|对内存中的依赖/子项的影响|对数据库中的依赖项/子项的影响
ClientSetNull(默认)|外键属性设置为空|无
EF Core 2.0 中的更改:在以前的版本中,Restrict 会导致被跟踪的依赖实体中的可选外键属性设置为 null,并且是可选关系的默认删除行为。在 EF Core 2.0 中,引入了 ClientSetNull 来表示该行为,并成为可选关系的默认值。 Restrict 的行为已调整为不会对依赖实体产生任何副作用。
任何关于为什么会发生这种情况的帮助将不胜感激。
【问题讨论】:
标签: entity-framework ef-code-first entity-framework-core ef-core-2.0