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 Post
s.
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…