I'm use EF core, and need advice, how correct create table. There is an article, it can be commented. It is also possible to comment on the comments themselves. How to describe the entities for the database?
So:
public class Article
{
public int Id {get; set;}
public string Content {get; set;}
public List<CommentForArticle>Comments{get;set;}
}
public class CommentForArticle
{
public int Id {get; set;}
public string Content {get; set;}
public List<CommentForComment> Comments {get; set;}
public int ArticleId {get; set;}
public Article Artcile {get; set;}
}
public class CommentForComment
{
public int Id {get; set;}
public string Content {get; set;}
public int CommentId {get; set;}
public CommentForArticle CommentForArticle{get; set;}
}
Or so:
public class Article
{
public int Id {get; set;}
public string Content {get; set;}
public List<Comment>Comments{get; set;}
}
public class Comment
{
public int Id {get; set;}
public string Content {get; set;}
public CommentType Type {get; set;}
public List<Comment> Comments {get; set;}
public int? ArticleId {get; set;}
public Article? Artcile {get; set;}
public int? CommentId {get; set;}
public Comment? Comment{get; set;}
}
public enum CommentType
{
CommentForArticle,
CommentForComment
}
Question is opinion-based and depends on your requirements. I would implement your logic with only two classes (Occam's razor) and maybe even without additional enumeration - you can just add a method with will check that one and only FK has a value - I guess that's the simplest approach.