Учитывая следующую модель ...
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; }
}
... мы можем активную загрузку с помощью Включить
Родителя
со всеми ] Дочерние
и Внуки
за один шаг следующим образом:
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
за один раз.
Заранее благодарим за отзыв!