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

≯℡__Kan透↙ 提交于 2019-12-11 10:58:48

问题


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.

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

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:


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.

Code First Conventions



来源:https://stackoverflow.com/questions/30033217/is-cascade-delete-automatically-performed-in-entity-framework-1-1-required-rel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!