c# - Loading grandchild object in child collection, EF -
this question has answer here:
- include grandchildren in ef query 2 answers
model:
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; } }
i have existing parent entity children collection. after operations want load newly added children objects parent's collection database this
_context.entry(parent).collection(f => f.children).load();
but when doing that, grandchild object in every newly added collection element null. tried include grandchild in way still grandchild object null.
_context.entry(parent).collection(f => f.children).query().include(c => c.grandchild).load();
how correctly load new items parent's children collection including grandchild objects?
edit: dont know why question marked duplicate? problem is: have existing (loaded/tracked) parent entity in 1 context instance (form) , in context instance (form) modified entity child's collection ( add or remove child). want load these newly added entries parent entity collection in first context instance using 1 of written methods after newly added object loaded grandchild's nulls. don't know how correctly load these new child objects existed (tracked) parent entity without getting nulls.
i use following(wihout load)
parent parent = _context.parent.include(p => p.children.grandchild).firstordefault();
and if grandchild collection, use
parent parent = _context.parent.include(p => p.children.select(c => c.grandchild).firstordefault();
Comments
Post a Comment