使用实体框架级联删除 [英] Cascading delete with Entity Framework

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

问题描述

我有一个评论表,其中包含列 Id - 注释的ID, Commenter_Id 发布它的用户的ID,以及 ParentComment_Id 一个自引用的外键。评论,家长和小评论只有两个层次。如果注释记录有一个 ParentComment_Id 为null,那么它是一个父注释。

I have a comments table which contains amongst other things the columns Id - the ID of the comment, Commenter_Id the ID of the user who posted it, and ParentComment_Id a self referencing foreign key. There are only two levels to the commenting, parents and sub comments. If a comment record has a ParentComment_Id of null, then it is a parent comment.

我试图写一个表达式来删除用户的所有注释,但是这是因为我之前提到的自我引用而导致问题。

I'm trying to write an expression to delete all the comments for a user, but this is causing problems because of that self reference I mentioned earlier.

以此记录示例为例问题:

Take this records sample as an example of the problem:

ID为2的用户发布了一个注释,ID为3.由于这是父注释,它的一个 ParentComment_Id 值为null。后来,ID为1的用户用一条子评论回应了该评论,创建了评论7(这两个之间还有其他评论/子评论,因此Id增加跳跃)。

User with ID 2 posted a comment, with an ID of 3. Because this was the parent comment it has a ParentComment_Id value of null. Later on, User with ID 1 responds to that comment with a sub-comment, creating comment 7 (there were other comments/subcomments between these two hence the Id increment jump).

我无法删除评论ID 3,因为子评论,评论ID 7,有一个外键。

I'm not able to delete Comment ID 3 because the sub comment, Comment ID 7, has a foreign key to it.

目前我的实体框架语句尝试删除评论如下:

Currently my Entity Framework statement for trying to delete comments is as follows:

context.Comments.Where(x => x.Commenter.Id == user.Id).Delete();

但是,由于描述的问题,这给我一个例外。

But this gives me an exception because of the described problem.

我可能会使用几个foreach循环来解决这个问题,但我希望有一个更简单的方法,如 context.Cascade()。Where(... 对于那些想知道 Delete()方法是EntityFramework.Extended软件包的一部分。

I could probably fix this using a few foreach loops, but I was hoping there is an easier way like context.Cascade().Where(.... For those wondering the Delete() method is part of the EntityFramework.Extended package.

推荐答案

如果你有一个这样的实体:

If you have an entity like this:

public class Comment
{
  public int Id{get;set;}
  public int? ParentCommentId{get;set;}

  public virtual Comment ParentComment{get;set;}
  public virtual ICollection<Comment> Comments{get;set;}
}

您可以尝试使用此配置:

You could try with this configuration:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Comment>()
                .HasOptional(c=>c.ParentComment)
                .WithMany(c=>c.Comments)
                .HasForeignKey(c => c.ParentCommentId) 
                .WillCascadeOnDelete(true);
}

您可以使用 WillCascadeOnDelete 方法。如果依赖实体上的外键为空,,Code First 不会在关系上设置级联删除,而主体被删除,外键将被设置为null。

You can configure cascade delete on a relationship by using the WillCascadeOnDelete method.If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.

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

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