简体   繁体   English

是否在Entity Framework 1-1(必需)关系中自动执行Cascade Delete?

[英]Is Cascade Delete automatically performed in Entity Framework 1-1 (required) Relationships?

Searching about the cascading behavior of Entity Framework , if noticed this answer. 如果注意到答案,则搜索Entity Framework的级联行为。 On short, the OP is searching for a way to perform cascade delete on 1-0 / 1-1 relationships in Entity Framework , and the best answer contains a snippet on Fluent API applying WillCascadeOnDelete(true); 简而言之,OP正在寻找一种方法来对Entity Framework 1-0 / 1-1关系执行级联删除,并且最佳答案包含使用WillCascadeOnDelete(true);在Fluent API上的代码片段WillCascadeOnDelete(true); On the DB Context's modelBuilder. 在数据库上下文的modelBuilder上。

On the selected answer, a user (CodeMonkey) mentioned: 在选定的答案上,一个用户(CodeMonkey)提到:

(...) Some have suggested [Required] for cascading delete. (...)有人建议[必需]级联删除。 This indeed works, but of course, only if it is actually required 这确实有效,但是当然只有在实际需要时

My question is, if the relation is set to 1-1 (using [Required] or even without setting the relation as optional to EntityTypeConfiguration ) with required dependencies, is cascade delete performed by default, without having to set it to Fluent API, as mentioned on the comment? 我的问题是,如果该关系设置为1-1(使用[Required]甚至没有将该关系设置为EntityTypeConfiguration可选项)且具有必需的依赖项,则默认情况下将执行级联删除,而不必将其设置为Fluent API,例如在评论中提到?

If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. 如果从属实体上的外键不可为空,则Code First将在关系上设置级联删除。 If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null. 如果从属实体上的外键可为空,则Code First不会在关系上设置级联删除,并且当删除主体时,外键将设置为null。 The multiplicity and cascade delete behavior detected by convention can be overridden by using the fluent API. 可以使用fluent API覆盖通过约定检测到的多重性和级联删除行为。

Code First Conventions 代码优先约定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM