简体   繁体   English

与实体框架级联删除

[英]Cascading delete with Entity Framework

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. 我有一个意见表,包含了除其他事项外的列Id -评论的ID, Commenter_Id谁贴吧,以及用户的ID ParentComment_Id自参考外键。 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. 如果注释记录的ParentComment_Id为null,则它是父注释。

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: 以以下记录示例为例:

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. ID为2的用户发布了ID为3的注释。由于这是父注释,因此其ParentComment_Id值为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为1的用户使用子注释来响应该注释,从而创建注释7(这两个注释之间存在其他注释/子注释,因此ID增量会跳跃)。

I'm not able to delete Comment ID 3 because the sub comment, Comment ID 7, has a foreign key to it. 我无法删除注释ID 3,因为子注释Comment ID 7具有外键。

Currently my Entity Framework statement for trying to delete comments is as follows: 目前,我用于尝试删除注释的Entity Framework语句如下:

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

But this gives me an exception because of the described problem. 但是由于所描述的问题,这给了我一个例外。

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. 我可能可以使用一些foreach循环来解决此问题,但我希望有一种更简单的方法,例如context.Cascade().Where(...对于那些想知道Delete()方法是EntityFramework.Extended包的一部分的人。

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);
}

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. 您可以使用WillCascadeOnDelete方法在关系上配置级联删除。如果从属实体上的外键可为空 ,则Code First 不会在关系上设置级联删除 ,并且当删除主体时,外键将设置为null 。

You can try adding this to your dbcontext 您可以尝试将其添加到dbcontext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Comments>()
                .HasForeignKey(d => d.ParentCommentId) 
                .WillCascadeOnDelete(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM