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 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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors. How do I solve this problem?