【问题标题】:Is there any way to get Child elements automatically based on two parameters with Entity Framework?有什么方法可以根据实体框架的两个参数自动获取子元素?
【发布时间】:2009-09-18 13:48:13
【问题描述】:

我有两个实体“文章”和“图片”,还有一个“评论”实体来保存任何文章和图片的 cmets。 (我知道我可以有两个用于 cmets 的表,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

如果我有两个用于 cmets 的表格,我的意思是 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 在不同的 CommentType 上可能相同(假设 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

那么是否可以在自定义枚举 CommentType 的帮助下将 cmets 保存在一个表中以用于两种不同类型的实体

【问题讨论】:

    标签: c# asp.net entity-framework


    【解决方案1】:

    是的,正如你所描述的那样是可能的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多