使用EF Core进行级联删除

编程入门 行业动态 更新时间:2024-10-27 01:20:02
本文介绍了使用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 )

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。

参考:

  • 关系-术语的定义
  • 层叠删除

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:

  • Relationships - Definition of terms
  • Cascade Delete

更多推荐

使用EF Core进行级联删除

本文发布于:2023-11-14 10:38:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级联   EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!