[英]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();
我应该如何重写此查询?
PS我使用的是EF 3.5(不是4.0)
这很可能与包含和联接的问题有关。 基本上可以归结为:includes仅在语句的末尾应用,由于连接的缘故,查询的类型从IQueryable<Message>
变为IQueryable<Comment>
。
通过删除联接,它应正确包含导航属性。 请尝试以下操作:
ctx.Comments
.Include("CommentType")
.Include("Owner")
.Include("Message")
.Where(c => c.Message.RID == messageId && c => c.CommentType.Id == commentTypeId)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.