考虑下面的模型孙子藏品的明确载入中...在EF 4.1
public class Parent
{
public int Id { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public ICollection<Grandchild> Grandchildren { get; set; }
}
public class Grandchild
{
public int Id { get; set; }
}
...我们能渴望负荷与Include
一个Parent
所有Children
在像这样一步Grandchildren
:
context.Parents.Include(p => p.Children.Select(c => c.Grandchildren))
是类似的可能明确加载?
子集可以明确地加载这样:
Parent parent = context.Parents.Find(parentId);
context.Entry(parent).Collection(p => p.Children).Load();
但尝试加载孩子以类似的方式与Include
...
context.Entry(parent)
.Collection(p => p.Children.Select(c => c.Grandchildren)).Load();
...不编译和字符串过载Collection
...
context.Entry(parent).Collection("Children.Grandchildren").Load();
...抛出一个例外(“......不允许虚线路径......”)。
,我发现工作的唯一一件事就是明确地加载Grandchildren
在一个循环:
Parent parent = context.Parents.Find(parentId);
context.Entry(parent).Collection(p => p.Children).Load();
foreach (var child in parent.Children)
context.Entry(child).Collection(c => c.GrandChildren).Load();
我想知道如果我错过了什么,如果有一些其他的方式来明确地加载在一个往返GrandChildren
。
感谢您的反馈!
您是否尝试过'Collection(...)。Query()。Include(...)。Load()'?如果它不工作,恐怕它不被支持。通常'Load'相当于处理来自ObjectContext API的'RelatedEnd.Load'。 – 2011-05-11 15:50:26
@Ladislav:请把这个答案:)它的工作原理! – Slauma 2011-05-11 15:58:51