Delete behaviors

Delete behaviors are defined in the DeleteBehavior enumerator type and can be passed to the OnDelete fluent API to control whether the deletion of a principal/parent entity or the severing of the relationship to dependent/child entities should have a side effect on the dependent/child entities.
There are three actions EF can take when a principal/parent entity is deleted or the relationship to the child is severed:

  • The child/dependent can be deleted
  • The child's foreign key values can be set to null
  • The child remains unchanged
Note
The delete behavior configured in the EF Core model is only applied when the principal entity is deleted using EF Core and the dependent entities are loaded in memory (that is, for tracked dependents). A corresponding cascade behavior needs to be setup in the database to ensure data that is not being tracked by the context has the necessary action applied. If you use EF Core to create the database, this cascade behavior will be setup for you.

For the second action above, setting a foreign key value to null is not valid if foreign key is not nullable. (A non-nullable foreign key is equivalent to a required relationship.) In these cases, EF Core tracks that the foreign key property has been marked as null until SaveChanges is called, at which time an exception is thrown because the change cannot be persisted to the database. This is similar to getting a constraint violation from the database.

There are four delete behaviors, as listed in the tables below.

Optional relationships
For optional relationships (nullable foreign key) it is possible to save a null foreign key value, which results in the following effects:

Behavior Name Effect on dependent/child in memory Effect on dependent/child in database
Cascade Entities are deleted Entities are deleted
ClientSetNull (Default) Foreign key properties are set to null None
SetNull Foreign key properties are set to null Foreign key properties are set to null
Restrict None None

Required relationships
For required relationships (non-nullable foreign key) it is not possible to save a null foreign key value, which results in the following effects:

Behavior Name Effect on dependent/child in memory Effect on dependent/child in database
Cascade (Default) Entities are deleted Entities are deleted
ClientSetNull SaveChanges throws None
SetNull SaveChanges throws SaveChanges throws
Restrict None None

In the tables above, None can result in a constraint violation. For example, if a principal/child entity is deleted but no action is taken to change the foreign key of a dependent/child, then the database will likely throw on SaveChanges due to a foreign constraint violation.

At a high level:

  • If you have entities that cannot exist without a parent, and you want EF to take care for deleting the children automatically, then use Cascade.

    • Entities that cannot exist without a parent usually make use of required relationships, for which Cascade is the default.
  • If you have entities that may or may not have a parent, and you want EF to take care of nulling out the foreign key for you, then use ClientSetNull
    • Entities that can exist without a parent usually make use of optional relationships, for which ClientSetNull is the default.
    • If you want the database to also try to propagate null values to child foreign keys even when the child entity is not loaded, then use SetNull. However, note that the database must support this, and configuring the database like this can result in other restrictions, which in practice often makes this option impractical. This is why SetNull is not the default.
  • If you don't want EF Core to ever delete an entity automatically or null out the foreign key automatically, then use Restrict. Note that this requires that your code keep child entities and their foreign key values in sync manually otherwise constraint exceptions will be thrown.

本文上面两个表格中的内容,主要说的是删除外键关系的主表数据后,不同的DeleteBehavior对从表数据的影响。这里还演示了从主表实体类的导航属性中删除从表实体,不同的DeleteBehavior对从表数据的影响,相信这也是很多人比较关心的一个问题。

原文链接

EF Core中外键关系的DeleteBehavior介绍(转自MSDN)的更多相关文章

  1. C# 数据操作系列 - 6 EF Core 配置映射关系

    0. 前言 在<C# 数据操作系列 - 5. EF Core 入门>篇中,我们简单的通过两个类演示了一下EF增删改查等功能.细心的小伙伴可能看了生成的DDL SQL 语句,在里面发现了些端 ...

  2. .ef core 多对对关系的关联方法

    最近在用.net core 重构博客,在使用ef core连表查询时,遇到了一些问题.记录一下. 关系:一个博客可以有多个标签,一个标签可以属于多个博客,博客和标签之间存在多对多的关系 下面是实体代码 ...

  3. EF Core 2.1 Raw SQL Queries (转自MSDN)

    Entity Framework Core allows you to drop down to raw SQL queries when working with a relational data ...

  4. EF Core 2.0 已经支持自动生成父子关系表的实体

    现在我们在SQL Server数据库中有Person表如下: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Cr ...

  5. EF Core反向导航属性解决多对一关系

    多对一是一种很常见的关系,例如:一个班级有一个学生集合属性,同时,班级有班长.语文课代表.数学课代表等单个学生属性,如果定义2个实体类,班级SchoolClass和学生Student,那么,班级Sch ...

  6. EF Core » 关系

    对初学者理解关系很有用,先留下来,有时间边看边翻译. Caution 注意 This documentation is for EF Core. For EF6.x and earlier relea ...

  7. [翻译] 介绍EF Core

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  8. C# 数据操作系列 - 8. EF Core的增删改查

    0.前言 到目前为止,我们看了一下如何声明EF Core的初步使用,也整体的看了下EF Core的映射关系配置以及导航属性的配置. 这一篇,我带大家分享一下,我在工作中需要的EF Core的用法. 1 ...

  9. EF Core 的关联查询

    0 前言 本文会列举出 EF Core 关联查询的方法: 在第一.二.三节中,介绍的是 EF Core 的基本能力,在实体中配置好关系,即可使用,且其使用方式,与编程思维吻合,是本文推荐的方式. 第四 ...

随机推荐

  1. sql:Mysql create view,function,procedure

    create database Liber; use Liber; #顯示數据庫 20150210 Geovin Du 涂聚文 SHOW DATABASES; drop table BookKindL ...

  2. sublime3下载安装及常用插件、浏览器预览设置

    之前与学习前端有关的软件都安装在了实验室电脑上,最近由于要放寒假(也许我寒假回去会学习呢),于是得在笔记本电脑上重新安装一遍.几个软件各种出错,花了一下午才安装好,必须记录下来啊! 这篇文章主要介绍s ...

  3. 【一些简单的jQuery选择器】

    学习[js DOM 编程艺术],最后面有许多jQuery的选择器,每个都动手敲了一遍. jQuery 提供了高级选择器的方法. js获取元素的三个基本方法分别是通过标签名,类名和id,即(getEle ...

  4. 微信小程序一个页面多个按钮分享怎么处理

    首先呢,第一步先看api文档: 组件:button https://developers.weixin.qq.com/miniprogram/dev/component/button.html 框架- ...

  5. HTML5-入门。

    什么是HTML5? HTML5是超文本语言,不是编程语言,html5是html语言的最新版本,需要注意浏览器的兼容性问题. HTML5技术一般是指的是HTML5.CSS3.JavaScript三种技术 ...

  6. 排查在 Azure 中新建 Windows 虚拟机时遇到的经典部署问题

    尝试创建新的 Azure 虚拟机 (VM) 时,遇到的常见错误是预配失败或分配失败. 当由于准备步骤不当,或者在从门户捕获映像期间选择了错误的设置而导致 OS 映像无法加载时,将发生预配失败. 当群集 ...

  7. iOS设计模式 - 适配器

    iOS设计模式 - 适配器 效果 说明 1. 为了让客户端尽可能的通用,我们使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信. 2. 本教程实现了适配器模式的类适配器与对象适配器两种 ...

  8. vs安装部署“必备”组件里面正常情况没有出现office2010 PIA组件

    vs安装部署“必备”组件里面正常情况没有出现office2010 pia组件时,将 Office2010PIARedist 文件夹放在C:\Program Files\Microsoft SDKs\W ...

  9. Exchange2016 & Skype for business 集成之一配置合作伙伴应用程序

    准备条件 为Skype for Business Server和Exchange Server之间建立服务器到服务器的身份验证,您必须做两件事:1)您必须为每台服务器分配合适的证书(详细参考文档htt ...

  10. CSS制作图形速查表

    很少会有人意识到,当浏览器绘制的border,会有一个角度的问题.我们就是得用这样的一个技巧来制作三角的效果.我们只需要保证一边的边框是有色,其他边框色为透明色,这样我们就很容易制作出三角形,然后改变 ...