实体框架&包括

时间:2011-02-06 20:10:20

标签: c# entity-framework include

所有 请帮我在下列情况下使include()工作:

ctx.Messages
  .Include("Comments.CommentType")
  .Include("Comments.Owner")
  .Include("Comments.Message")
  .Where(m => m.RID == messageId)
  .SelectMany(m => m.Comments)
  .Where(c => c.CommentType.Id == commentTypeId)
  .ToList();

我应该如何重写此查询?

P.S。我正在使用EF 3.5(不是4.0)

1 个答案:

答案 0 :(得分:1)

这很可能与包含和加入的issue有关。基本上它归结为:包含仅在语句末尾应用,并且由于加入,查询类型从IQueryable<Message>更改为IQueryable<Comment>

通过删除连接,它应该正确包含导航属性。请尝试以下方法:

ctx.Comments
   .Include("CommentType")
   .Include("Owner")
   .Include("Message")
   .Where(c => c.Message.RID == messageId && c => c.CommentType.Id == commentTypeId)
   .ToList();