我有两个实体“文章”和“图片”,并且有一个“评论”实体来保存任何文章和图片的评论。 (我知道我可以有两个评论表,ArticleComment和PictureComment )
评论表结构是;
Id numeric(18,0) System.Long
CommentType byte CommentType (CommentType.Article, CommentType.Picture)
ItemId numeric(18,0) System.Long
Body nvarchar(max) string
如果我有两个评论表,我的意思是ArticleComment和PictureComment,我就可以做到这一点
db.Article.Include("ArticleComment").FirstOrDefault(a=>a.id == articleId);
但如果我只有一张评论表,我就无法做到这一点。因为我需要指定CommentType。
我想我可以为这样的文章或图片实体检索评论吗?
db.Article.Include("Comment").FirstOrDefault(a=>a.id == itemId && a.CommentType == CommentType.Article);
但是保存注释时可能会出现问题,因为itemId在不同的CommentTypes上可以是相同的(假设Comment.ItemId与数据库中的Picture.Id和Article.Id相关联,因此在EF模型中)
注释
Id CommentType ItemId Comment
--- ----------- ------ -------
1 1 5 Comment for 5th Picture
2 2 5 Comment for 5th Article
因此,可以通过Custom Enum CommentType帮助在一个表中为两种不同类型的实体保留注释
答案 0 :(得分:0)
是的,完全可以像你描述的那样。