简体   繁体   中英

EF 6 complex query with Include not including relations

i have a somewhat complex structure i wont get into, but what i try doing is:

Get all ShopItems, who's SourceItem has changed, Get and update them according to their Source/Shop data.

i conjured the following:

var query = _ctx.ShopItems
            .Include(si => si.Shop)
            .Include(si=>si.SourceItem)
            .Include(si => si.SourceItem.Source)
            .Include(si=>si.Shop.ShopType)
            .GroupBy(i => i.SourceItem)
            .Where(g => g.Key.LastUpdate > lastUpdate)
            .OrderBy(g => g.Key.LastUpdate)
            .Take(updateCountLimit);

the query seems to work, but when itterating the Groups:

groupItem.Key.Source is null.

I somewhat solved it by Removing the Include() s, saving the Entities to an Array, and explicitly loading the references using _ctx.Entry(updatedSourceItem.Key).Reference(src=>src.Source).Load();

How can i perform the query i want without round-tripping the DB for explicit loading ?

Not sure, but it's backwards to start with ShopItems and then group by SourceItem. Try just starting with SourceItem, something like

:

    var query = _ctx.SourceItems
                    .Include(i => i.ShopItems)
                    .Include(i => i.Source)
                    .Include(i => i.ShopItems.Select( si => si.Shop))
                    .Include(i => i.ShopItems.Select( si => si.Shop).ShopType)
                    .Where(i => i.LastUpdate > lastUpdate)
                    .OrderBy(i => i.LastUpdate)
                    .Take(updateCountLimit);
//or 

    var query = _ctx.SourceItems
                    .Include("ShopItems")
                    .Include("Source")
                    .Include("ShopItems.Shops")
                    .Include("ShopItems.Shops.ShopType")
                    .Where(i => i.LastUpdate > lastUpdate)
                    .OrderBy(i => i.LastUpdate)
                    .Take(updateCountLimit);

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