简体   繁体   English

关于在实体框架中级联删除的说明

[英]Explanation about Cascade on Delete in Entity Framework

I need a thorough explanation on cascade on delete because it is giving me unnecessary headache. 我需要对删除级联进行彻底的解释,因为它使我不必要地头痛。 I have a class News.cs and a class Comment.cs. 我有一类News.cs和一类Comment.cs。 News has a collection of comments and a comment must belong to a News, so I set up my class like below 新闻有评论集,评论必须属于新闻,因此我像下面这样设置班级

public class News
{
    public int NewsId { get; set; }

    [Display(Name = "Title")]
    public string Title { get; set; }

    [Display(Name = "Details")]
    public string Details { get; set; }

    public DateTime DateCreated { get; set; }

    public int AppUserId { get; set; }

    [ForeignKey("AppUserId")]
    public virtual AppUser AppUser { get; set; }

    public ICollection<Comment> Comment { get; set; }

}

public class Comment
{
    public int CommentId { get; set; }

    public string CommentText { get; set; }

    public DateTime DateCreated { get; set; }

    public int AppUserId  { get; set; }

    public int NewsId { get; set; }

    [ForeignKey("AppUserId")]
    public virtual AppUser User { get; set; }

    [ForeignKey("NewsId")]
    public virtual News News { get; set; }

}

The behaviour I'm expecting is that if I delete a comment it shouldn't affect the parent news but if I delete a news I don't see any reason to retain the children comments so comments should be deleted. 我期望的行为是,如果我删除评论,则不会影响父级新闻,但是如果删除新闻,我看不到保留子级评论的任何理由,因此应删除评论。 I ran an update database command in package manager console and I kept getting this error 我在程序包管理器控制台中运行了一个更新数据库命令,但不断出现此错误

Introducing FOREIGN KEY constraint 'FK_dbo.Comments_dbo.News_NewsId' on table 'Comments' may cause cycles or multiple cascade paths. 在表“注释”上引入外键约束“ FK_dbo.Comments_dbo.News_NewsId”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。 How do I solve this problem? 我该如何解决这个问题?

This error is caused when by deleting one entity, another entity will be deleted more that once. 通过删除一个实体,另一个实体将被删除一次以上,就会导致此错误。

In your scenario, if you delete AppUser with cascade delete turned on, this will attempt to delete the dependent entities, News and Comment. 在您的方案中,如果在启用级联删除的情况下删除AppUser,这将尝试删除相关实体(新闻和评论)。 As Comment is also dependent on News, When News is deleted, Comment will be deleted as a dependent (again). 由于评论也依赖于新闻,因此删除新闻时,评论也将作为从属而被删除。 As it may have already been deleted due to the dependency on AppUser, SQL cannot guarantee that the entity now exists, so SQL Server will prevent you from implementing this in the first place. 由于对AppUser的依赖关系,它可能已被删除,因此SQL无法保证该实体现在存在,因此SQL Server首先将阻止您实现该实体。

To resolve, the simplest thing is to turn off cascade delete on one or more of the dependents via the fluent api: 要解决此问题,最简单的方法是通过流畅的api关闭一个或多个依赖项上的级联删除:

modelBuilder.Entity<AppUser>().HasMany(au => au.Comments)
.WithRequired(c => c.AppUser)
.HasForeignKey(c => c.AppUserID)
.WillCascadeOnDelete(false);

modelBuilder.Entity<AppUser>().HasMany(au => au.News)
.WithRequired(n => n.AppUser)
.HasForeignKey(n => n.AppUserID)
.WillCascadeOnDelete(false);

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

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