public class Parent
{
public int Id { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public int IdGrandChild { get; set; }
public virtual Grandchild Grandchild { get; set; }
}
public class Grandchild
{
public int Id { get; set; }
}
我有一個孩子收集現有的父實體。一些操作我想加載新加入的孩子後,反對家長收集來自數據庫這樣
_context.Entry(parent).Collection(f => f.Children).Load();
但是,當我這樣做那樣,在每一個新加入的集合元素孫子對象爲null。我也嘗試過以這種方式包含孫子,但孫子對象仍爲空。
_context.Entry(parent).Collection(f => f.Children).Query().Include(c => c.Grandchild).Load();
如何正確加載新項目到Parent's Children集合,包括Grandchild對象?編號: 我不知道爲什麼這個問題被標記爲重複? 我的問題是:我已經在一個上下文實例(表單)中存在(加載/跟蹤的)父實體,然後在另一個上下文實例(表單)中修改實體子集合(添加或刪除子進程)。最後,我想用第一個上下文實例中的一個,將這些新添加的條目加載到第一個上下文實例中的父實體集合中,但在此之後,新加入的對象被加載,但是其孫子的空值爲空。 我不知道如何正確加載這些新的子對象到已存在的(已跟蹤的)父實體而不會得到空值。
是[這](http://stackoverflow.com/ question/10822656/entity-framework-include-multiple-levels-of-properties)你在找什麼? – Robert
http://stackoverflow.com/questions/10822656/entity-framework-include-multiple-levels-of-properties – Moho
如何在已經跟蹤的實體上使用它? _context.Entry(父)???。 –