我有以下查询:
var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
where e.Customers.ID == customer.ID
select e;
一切正常,我得到了我的设备,它正确加载制造商表(热切地)。但是,当我尝试执行以下多对多查询时:
var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
where e.Customers.ID == customer.ID
from cce in e.ContractEquipments
where cce.Contracts.EndedOn >= DateTime.Today
select e;
其中“ContractEquipments”是“设备”和“合同”之间的多对多查找,但是当此查询运行时,制造商表格不再容易加载。知道如何在不执行以下操作的情况下解决此问题:
if (MyEntity.Manufacturers.IsLoaded == false)
MyEntity.ManufacturersReference.Load()
这个项目需要几个小时执行,我想保持数据库调用的数量。
编辑#1:
我也尝试了这个没有成功:
var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
where e.Customers.ID == customer.ID
join cce in ContractContext.ContractEquipments
on e.ID equals cce.Equipments.ID
where cce.Contracts.EndedOn >= DateTime.Today
select e;
答案 0 :(得分:7)
早期的包含通常会在某些类型的查询中丢失(即使用额外的连接等)
解决这个问题的方法是进行查询,(然后只要您返回实体,即选择e而不是投影,即选择新的{...}),您可以转换为ObjectQuery并执行包含在外面:
var MyQuery = ((from e in ContractContext.Equipments
where e.Customers.ID == customer.ID
from cce in e.ContractEquipments
where cce.Contracts.EndedOn >= DateTime.Today
select e) as ObjectQuery<Equipment>).Include("Manufacturers");
这应该有用。
如果您对此感兴趣,请查看Tip 22 - How to make Include really Include
亚历
答案 1 :(得分:0)
您是否尝试过这样的加入?
var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
where e.Customers.ID == customer.ID
join cce in e.ContractEquipments on e.Id equals cce.EquipmentId
where cce.Contracts.EndedOn >= DateTime.Today
select e;