使用EF Core进行级联删除 [英] Cascade deleting with EF Core

查看:745
本文介绍了使用EF Core进行级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我在EF Core上遇到了一些问题。我有一些数据需要删除,而我正努力查看fluent API的工作原理,确切地说是关于 .OnDelete()函数。



考虑来自微软自己的网站,我想知道 OnDelete()到底是哪个目标(由于缺少更好的词)实例似乎是博客,在其他情况下则是帖子。如果可以,我是否可以从两侧定义级联删除(当父Blog为Blog时删除帖子),如果我想象代码应该像这样:



model.Entity< Post>()。HasOne(p => p.Blog).WithMany(b => b.Posts).HasForeignKey(p => p.BlogId).OnDelete(DeleteBehavior.Cascade) )



据我了解,这是删除博客后,请先删除所有引用此博客的帖子,即 OnDelete(DeleteBehavior.Cascade)应用于博客,而不是发布。



但是这是一样的吗?



model.Entity< Blog>()。HasMany(b => b.Posts).WithOne(p => p.Blog).OnDelete(DeleteBehavior .Cascade)



还是 OnDelete(DeleteBehavior.Cascade)适用于Post

解决方案

级联删除始终在一个方向上起作用-从主要实体相关实体,即删除主实体删除从属实体。对于一对多关系, one 一侧始终是 principal ,而 many 一侧始终是依赖



看起来您对流畅的配置感到困惑。请注意,每个关系都包含两个目的。流利的配置使您可以从一端开始并将其与另一端相关,反之亦然,但是您仍然在配置(定义)关系。因此

  Entity< A>()。HasOne(a => aB).WithMany(b => b.As )



<$ p $相同p> Entity< B>()。HasMany(b => b.As).WithOne(a => aB);

它们都定义了一个相同的关系。选择哪个无关紧要,只需为每个关系使用单一配置,以免出现差异。



话虽如此,

  model.Entity< Post>()。HasOne(p => p.Blog).WithMany(b => b.Posts)
。 HasForeignKey(p => p.BlogId)
.OnDelete(DeleteBehavior.Cascade);

  model.Entity< Blog>()。HasMany(b => b.Posts).WithOne(p => p.Blog)
.HasForeignKey(p => p.BlogId)
.OnDelete(DeleteBehavior.Cascade);

是一个相同的对象,并定义单个一对多关系从博客 Post 。由于 Blog one 一方,而 Post 许多方面,博客主要实体,而 Post 相关实体,因此删除博客会删除相关的帖子 s。



参考:




  • 关系-术语的定义

  • < a href = https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete rel = noreferrer>层叠删除


I am having a few issues with EF Core at the moment. I have some data that I need to delete, and I am struggeling to see how the fluent API works, exactly in regards to the .OnDelete() function.

Considering the classic blog/post scenario from microsofts own websites, I wonder what entity, exactly the OnDelete() is 'targeting' (for the lack of a better word) In some instances it seems to be the blog, in others, the post. Can the Cascade delete be defined from both sides (that the posts are deleted when the parent Blog is) if so i imagine the code should look like this:

model.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts).HasForeignKey(p => p.BlogId).OnDelete(DeleteBehavior.Cascade)

As i understand this is saying "When a Blog is deleted, first delete all posts referencing this blog" meaning the OnDelete(DeleteBehavior.Cascade)applies to blog, not to post.

But is this the same then?

model.Entity<Blog>().HasMany(b => b.Posts).WithOne(p => p.Blog).OnDelete(DeleteBehavior.Cascade)

or does OnDelete(DeleteBehavior.Cascade) apply to Post rather than blog?

解决方案

Cascade delete always works in one direction - from principal entity to dependent entity, i.e. deleting the principal entity deletes the dependent entities. And for one-to- many relationships the one side is always the principal and the many side is the dependent.

Looks like you are confused by the fluent configuration. Note that each relationship consists of two ends. The fluent configuration allows you to start with one of the ends and relate it to the other end, or vice versa, but still you are configuring (defining) a single relationship. So

Entity<A>().HasOne(a => a.B).WithMany(b => b.As)

is the same as

Entity<B>().HasMany(b => b.As).WithOne(a => a.B);

and they both define one and the same relationship. Which one you choose doesn't matter, just use single configuration per relationship in order to avoid discrepancies.

With that being said,

model.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts)
    .HasForeignKey(p => p.BlogId)
    .OnDelete(DeleteBehavior.Cascade);

and

model.Entity<Blog>().HasMany(b => b.Posts).WithOne(p => p.Blog)
    .HasForeignKey(p => p.BlogId)
    .OnDelete(DeleteBehavior.Cascade);

is one and the same and define single one-to-many relationship from Blog to Post. Since Blog is the one side and Post is the many side, the Blog is the principal entity and the Post is the dependent entity, hence deleting a Blog will delete the related Posts.

Reference:

这篇关于使用EF Core进行级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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