简体   繁体   English

具有Include不包括关系的EF 6复杂查询

[英]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. 获取所有SourceItem已更改的ShopItem,并根据其Source / Shop数据获取并更新它们。

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. groupItem.Key.Source为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(); 我通过移除Include() ,将实体保存到数组并使用_ctx.Entry(updatedSourceItem.Key).Reference(src=>src.Source).Load();显式加载引用来_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. 不确定,但是从ShopItems开始,然后再按SourceItem分组是倒退的。 Try just starting with SourceItem, something like 尝试仅从SourceItem开始,例如

:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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