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