使用显式加载使用现有ChildIds加载集合

时间:2016-10-05 11:58:59

标签: c# entity-framework entity-framework-6

是否可以使用现有的Child Ids显式重新加载Reload()DbEntityEntry的功能?另见代码中的注释。

using (var context = new MyContext())
{
    var parent = new Parent()
    {
        ParentId = 1,
        Childs = new List<Child>()
        {
            new Child()
            {
                ChildId = 2,
                ParentId = 1
            }
        }
    };

    context.Parents.Attach(parent);
    context.Entry(parent)
        .Collection(b => b.Childs) // Load only Employee with employee id of 2
        .Query()
        .Load(); //Is it possible to Reload only ChildId = 2?
}

1 个答案:

答案 0 :(得分:1)

是的,可以在Query方法之后将过滤器应用于您要加载的相关实体:

 context.Entry(parent)
        .Collection(b => b.Childs) // Load only Employee with employee id of 2
        .Query()
        .Where(e=>e.ChildId==2)
        .Load();