Tag: navigational properties

在实体上显式加载多个引用/集合

考虑以下实体模型: public class Parent { public virtual FirstChild FirstChild { get; set; } public virtual SecondChild SecondChild { get; set; } } 在我的代码中,我加载了Parent实体: Parent parent = ; 为了明确加载其导航属性,我使用 db.Entry(parent).Reference(p => p.FirstChild).Load(); db.Entry(parent).Reference(p => p.SecondChild).Load(); 但这导致两个DB查询。 问题:是否有更优雅的方式,允许在单个查询中显式加载多个导航属性? 如果我没有parent加载,我会急切加载: Parent parent = db.Parents .Include(p => p.FirstChild) .Include(p => p.SecondChild) .FirstOrDefault(); 但是,正如我所提到的,我已经加载了没有相关实体(我无法修改加载代码)。

基于抽象实体的急切加载嵌套导航问题(EF CTP5)

我的EF模型的一部分看起来像这样: 摘要: 位置有很多post Post是一个抽象类 讨论来自Post 讨论有很多评论 现在,我正试图实现的查询: 获取有关位置ID 1234的信息,包括与这些讨论相关的任何讨论和评论。 我可以得到讨论和这样的评论: var discussions = ctx.Posts .OfType() .Include(x => x.Comments) .ToList(); 但我似乎无法根据位置实体上的post导航获取它。 我试过这个: var locationWithDiscussionsAndComments = ctx .Locations .Include(x => x.Posts .OfType() .Select(y => y.Comments)) .SingleOrDefault(); 哪个编译,但我得到错误: System.ArgumentException:包含路径表达式必须引用实体定义的属性,也可以引用嵌套属性或对Select的调用。 参数名称:路径 有任何想法吗? 我可能会从post中“倒退”: var locationWithDiscussionsAndComments = ctx .Posts .Include(x => x.Location) .OfType() .Include(x => x.Comments) .Where(x => x.LocationId == […]