簡體   English   中英

具有Include不包括關系的EF 6復雜查詢

[英]EF 6 complex query with Include not including relations

我的結構有點復雜,但是我嘗試做的是:

獲取所有SourceItem已更改的ShopItem,並根據其Source / Shop數據獲取並更新它們。

我想到了以下幾點:

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

該查詢似乎可以正常工作,但在修改網上論壇時:

groupItem.Key.Source為null。

我通過移除Include() ,將實體保存到數組並使用_ctx.Entry(updatedSourceItem.Key).Reference(src=>src.Source).Load();顯式加載引用來_ctx.Entry(updatedSourceItem.Key).Reference(src=>src.Source).Load();

我如何執行我想要的查詢,而又無需往返數據庫來進行顯式加載?

不確定,但是從ShopItems開始,然后再按SourceItem分組是倒退的。 嘗試僅從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