级联删除所有使用 Entity Framework 具有一对多和多对多关系的核心的子项
Cascade delete all children using Entity Framework Core with One-to-Many and Many-To-Many relationships
我有以下带有 entity framework 核心解决方案的 .net core 2 的简化架构:
1+--------+1
+-------+Blogs +-------+
| +--------+ |
*| |*
+--------+ +----------+
|Posts | |Tags |
+--------+ +----------+
|1 1|
| *+-------------+* |
+----+PostTags |------+
+-------------+
具有以下型号:
public class Blog
{
public int Id { get; set; }
public ICollection<Post> Posts { get; set; }
public ICollection<Post> Tags { get; set; }
}
public class Post
{
public int Id { get; set; }
public Blog Blog { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class Tag
{
public int Id { get; set; }
public Blog Blog { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public int TagId { get; set; }
public Tag Tag { get; set; }
}
以及对应的DbContext:
public class DataDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<PostTag> PostTags { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(u => new { pt.TagId, pt.PostId });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
}
级联删除有效,但 Tags
博客无效。如果我 [Require]
标签中的博客,我收到一个循环约束错误。
我想我自己找到了答案:
"In SQL Server, a table cannot appear more than one time in a list of
all the cascading referential actions that are started by either a
DELETE or an UPDATE statement. For example, the tree of cascading
referential actions must only have one path to a particular table on
the cascading referential actions tree
EF Core 中是否有一个优雅的解决方案来确保在删除博客后立即删除所有标签和帖子?
Is there an elegant solution for this in EF Core to ensure all Tags and Posts get deleted as soon as a Blog is being deleted?
您的两个选择是:
1)有一个Posts->PostTags级联删除和Blogs->BlogTags级联删除,先删除所有Blog的帖子,再删除博客。
2) 在博客上编写一个 INSTEAD OF DELETE 触发器,以在删除博客之前删除相关的帖子和 BlogTags。
我有以下带有 entity framework 核心解决方案的 .net core 2 的简化架构:
1+--------+1
+-------+Blogs +-------+
| +--------+ |
*| |*
+--------+ +----------+
|Posts | |Tags |
+--------+ +----------+
|1 1|
| *+-------------+* |
+----+PostTags |------+
+-------------+
具有以下型号:
public class Blog
{
public int Id { get; set; }
public ICollection<Post> Posts { get; set; }
public ICollection<Post> Tags { get; set; }
}
public class Post
{
public int Id { get; set; }
public Blog Blog { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class Tag
{
public int Id { get; set; }
public Blog Blog { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public int TagId { get; set; }
public Tag Tag { get; set; }
}
以及对应的DbContext:
public class DataDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<PostTag> PostTags { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(u => new { pt.TagId, pt.PostId });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
}
级联删除有效,但 Tags
博客无效。如果我 [Require]
标签中的博客,我收到一个循环约束错误。
我想我自己找到了答案:
"In SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree
EF Core 中是否有一个优雅的解决方案来确保在删除博客后立即删除所有标签和帖子?
Is there an elegant solution for this in EF Core to ensure all Tags and Posts get deleted as soon as a Blog is being deleted?
您的两个选择是:
1)有一个Posts->PostTags级联删除和Blogs->BlogTags级联删除,先删除所有Blog的帖子,再删除博客。
2) 在博客上编写一个 INSTEAD OF DELETE 触发器,以在删除博客之前删除相关的帖子和 BlogTags。