包含不包含关系的 EF 6 复杂查询
EF 6 complex query with Include not including relations
我有一个有点复杂的结构,我不会进入,
但我尝试做的是:
获取所有ShopItems,谁的SourceItem发生了变化,
根据他们的 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
为空。
我通过删除 Include()
s,将实体保存到数组,并使用显式加载引用来解决它
_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);
我有一个有点复杂的结构,我不会进入, 但我尝试做的是:
获取所有ShopItems,谁的SourceItem发生了变化, 根据他们的 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
为空。
我通过删除 Include()
s,将实体保存到数组,并使用显式加载引用来解决它
_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);