是否在Entity Framework 1-1(必需)关系中自动执行Cascade Delete? [英] Is Cascade Delete automatically performed in Entity Framework 1-1 (required) Relationships?

查看:73
本文介绍了是否在Entity Framework 1-1(必需)关系中自动执行Cascade Delete?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索实体框架的级联行为,如果发现答案。简而言之,OP正在寻找一种方法来对 Entity Framework 中的1-0 / 1-1关系执行级联删除,并且最佳答案包含Fluent API上的代码段在数据库上下文的modelBuilder上应用 WillCascadeOnDelete(true);

Searching about the cascading behavior of Entity Framework, if noticed this answer. 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); On the DB Context's modelBuilder.

在所选答案上,一个用户(CodeMonkey)提到:

On the selected answer, a user (CodeMonkey) mentioned:


(...)有人建议[必需]级联删除。这确实有效,但当然,只有在确实需要时,

(...) Some have suggested [Required] for cascading delete. This indeed works, but of course, only if it is actually required

我的问题是,如果关系设置为1-1 (使用[Required]甚至不将关系设置为 EntityTypeConfiguration 的可选项)具有必需的依赖项,默认情况下会执行级联删除,而不必将其设置为Fluent API,例如

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?

推荐答案


如果从属实体上的外键不可为空,则代码优先对关系设置级联删除。如果从属实体上的外键可为空,则Code First不会在关系上设置级联删除,并且当删除主体时,外键将设置为null。可以使用fluent API覆盖按惯例检测到的多重性和级联删除行为。

If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. 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. The multiplicity and cascade delete behavior detected by convention can be overridden by using the fluent API.

代码优先约定

这篇关于是否在Entity Framework 1-1(必需)关系中自动执行Cascade Delete?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆