EF core:关联对象:删除

DeleteBehavior

@想一想@:在关联的entity之间,当删除父entity(比如Classroom)的时候,子entity(比如Student)应该如何处理?

复习:__SQL中的Cascade Delete

自定义:OnDelete()方法

在OnModelCreating()中可调用OnDelete()方法,传递DeleteBehavior枚举值,指定EF core如何操作。

比如:

modelBuilder.Entity() .HasOne(s => s.SleepIn) .WithOne(b => b.Owner) .HasForeignKey(s => s.SleepInId) .OnDelete(DeleteBehavior.Cascade) ;

EF的DeleteBehavior枚举可以被归为3类。对应着当删除父entity的时候,子entity或数据行的3种处理方案:

  1. Cascade/ClientCascade:子entity被同时删除
  2. SetNull/ClientSetNull:其外键被设置为null (如果外键不能被设置为null,会报异常)
  3. NoAction:不发生改变

Client和非Client

比如DeleteBehavior.SetNull和DeleteBehavior.ClientSetNull:

  • client指EF,即:EF会生成处理子entity的Update/Delete语句,交数据库执行,不依赖于数据库的设定
  • 非client,需依靠数据库上的设定,EF不会生成处理子entity的Update/Delete语句

所以,如果设置是:

  • Client:子entity要已经加入DbContext (比如1:n的时候,n要双向引用且被加载到DbContext)
  • 非Client:数据库要有相应的Cascade设置。如果通过Migration或EnsureCreated创建的数据库,数据库上的Cascade会自动配置……

演示:

  1. 使用EF的migration或者API建库建表,不同的DeleteBehavior生成不同的SQL建表语句,比如:

    1. __DeleteBehavior.SetNull,生成ON DELETE SET NULL

      ALTER TABLE [dbo].[Students] ADD CONSTRAINT [FK_Students_Bed_SleepInId] FOREIGN KEY ([SleepInId]) REFERENCES [dbo].[Bed] ([Id]) ON DELETE __SET NULL__;

      __

    2. DeleteBehavior.ClientCascade,没有ON DELETE后缀 (_@想一想@_:为什么?)
  2. 删除一个父entity,在Logger中查看是否有删除/更新子entity的SQL语句

注意:如果说EF的Cascade配置和数据库设置相冲突,会报异常。

默认行为

EF根据子entity是否必须要有一个父entity(即数据库上外键是否可以为Null),默认设置Behavior:

  • 如果外键可以为空:DeleteBehavior.ClientSetNull
  • 如果外键不可以为空:DeleteBehavior.Cascade

标记删除

当entity之间的关系复杂之后,级联删除会导致复杂的链式反应,比如:

删除A就会删除B,删除B就会删除C和D,删除D又会删除E和F……但并不是每个entity都能删得掉

所以我们通常都不进行物理删除,而是用flag标记删除(在entity中添加一个是否已删除的Flag列),最后再由DBA批量删除,其优势为:

  • 减少用户等待时间
  • 可以回溯撤销
  • 便于控制意外情况发生

删除关系

有时候我们并不需要删除entity,只是想“切断”entity之间的关系。比如:1号教室原来是lw、zl、lzb三个同学,现在将lzb移出该教室(而不是删除)。

注意首先要把关联entity加载到DbContext中来。

SetNull和Remove

如果引用是单向的话,无论关联entity是集合还是单个元素。

小bug

使用LazyLoad,比如在“多对多”关系中,你可能会以为这样的代码是“重新赋值”

但查看运行结果,竟然是“添加”了。这是怎么一回事呢?https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...
https://github.com/hdfgfgh546...
https://www.github.com/hdfgfg...
http://github.com/hdfgfgh546/...

你可能感兴趣的:(sql)