简体   繁体   中英

Load collections with existing ChildIds using explicit loading

Is it possible to explicit reload using existing Child Ids something like functionality of Reload() in DbEntityEntry ? See also comments in code.

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?
}

Yes, it's possible, after Query method you can apply filters to the related entities you want to load:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM