0

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?

Hooman Limouee
  • 1,143
  • 2
  • 21
  • 43
  • look at recursive queries http://stackoverflow.com/questions/16749095/sql-recursive-query-that-gets-all-ancestors-of-an-item – Muckeypuck Mar 15 '17 at 20:00
  • Already know about recursive queries. But I haven't any clue about how do I do that using Code First @Muckeypuck – Hooman Limouee Mar 15 '17 at 20:17

0 Answers0