- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行 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; }
}
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);
});
https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete
Behavior Name | Effect on dependent/child in memory | Effect on dependent/child in database
ClientSetNull (Default) | Foreign key properties are set to null | None
Changes in EF Core 2.0: In previous releases, Restrict would cause optional foreign key properties in tracked dependent entities to be set to null, and was the default delete behavior for optional relationships. In EF Core 2.0, the ClientSetNull was introduced to represent that behavior and became the default for optional relationships. The behavior of Restrict was adjusted to never have any side effects on dependent entities.
最佳答案
EF Core 2.0.1 元数据和迁移使用不同的枚举来指定删除行为 - 分别 DeleteBehavior
和 ReferentialAction
.虽然第一个有据可查,但第二个和两者之间的映射没有(在撰写本文时)。
这是当前的映射:
DeleteBehavior ReferentialAction
============== =================
Cascade Cascade
ClientSetNull Restrict
Restrict Restrict
SetNull SetNull
DeleteBehavior
按照惯例是
ClientSetNull
映射到
onDelete: Restrict
,或者换句话说,强制(启用)FK 无级联删除。
modelBuilder.Entity<BigAwesomeDinosaurWithTeeth>()
.HasMany(e => e.YummyPunyPrey)
.WithOne(e => e.BigAwesomeDinosaurWithTeeth)
.OnDelete(DeleteBehavior.SetNull); // or whatever you like
关于entity-framework - EF Core 可为空关系设置删除 : ReferentialAction. 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521939/
我正在运行 efcore 2.0.1。 我有一个模型: public class BigAwesomeDinosaurWithTeeth { [Key] [DatabaseGenera
我是一名优秀的程序员,十分优秀!