Uso il nucleo EF e ho bisogno di consigli, quanto è corretto creare la tabella. C'è un articolo, può essere commentato. È anche possibile commentare i commenti stessi. Come descrivere le entità per il database?
Così:
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;}
}
O così:
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
}
La domanda è basata sull'opinione e dipende dalle tue esigenze. Implementerei la tua logica con solo due classi (il rasoio di Occam) e forse anche senza un'ulteriore enumerazione - puoi semplicemente aggiungere un metodo per verificare che uno e solo FK abbia un valore - immagino che sia l'approccio più semplice.