考慮下面的模型孫子藏品的明確載入中...在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