I'm working on a project (Asp.Net MVC CodeFirst
) with threaded comment system.
here is the comment model :
public class Comment : EntityBase {
public string FirstName { get; set; }
public string Email { get; set; }
public string Body { get; set; }
public int? ParentID { get; set; }
public bool HasChild { get; set; }
// Association Handling
[ForeignKey("Post")]
public virtual int PostID { get; set; }
public virtual Post Post { get; set; }
public virtual Comment CommentReference { get; set; }
}
and in the OnModelCreating() method :
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Comment>()
.HasOptional(c => c.CommentReference)
.WithMany()
.HasForeignKey(c => c.ParentID);
// Here I can't set cascade on delete to true (cause of cycle)
//.WillCascadeOnDelete(true);
}
the question is how do I delete a comment with its replies by passing the (Id)
of the comment?