LINQ到实体框架多对多的预先加载的问题实体、框架、加载、问题

由网友(周杰棍的双节伦)分享简介:我有以下查询:VAR更改为MyQuery =从E在ContractContext.Equipments.Include(制造商)其中,e.Customers.ID == customer.ID选择e;和一切正常,我让我的设备,并将其正确地装入厂家表(急切地)。但是,当我尝试做以下许多一对多查询:VAR更改为MyQuer...

我有以下查询:

  VAR更改为MyQuery =从E在ContractContext.Equipments.Include(制造商)
              其中,e.Customers.ID == customer.ID
              选择e;
 

和一切正常,我让我的设备,并将其正确地装入厂家表(急切地)。但是,当我尝试做以下许多一对多查询:

  VAR更改为MyQuery =从E在ContractContext.Equipments.Include(制造商)
              其中,e.Customers.ID == customer.ID
              从CCE在e.ContractEquipments
              其中,cce.Contracts.EndedOn> = DateTime.Today
              选择e;
 

,其中ContractEquipments是设备和合同,但此查询运行时,各厂家表不再被轻易装之间的许多一对多查找。任何想法如何解决这一问题没有具体操作如下:

 如果(MyEntity.Manufacturers.IsLoaded ==假)
   MyEntity.ManufacturersReference.Load()
 
LINQ to SQL 建立实体类

这个项目需要时间执行,我想保持数量的数据库调用了。

编辑#1:

我也试过这个没有成功:

  VAR更改为MyQuery =从E在ContractContext.Equipments.Include(制造商)
              其中,e.Customers.ID == customer.ID
              加入CCE在ContractContext.ContractEquipments
                在e.ID等于cce.Equipments.ID
              其中,cce.Contracts.EndedOn> = DateTime.Today
              选择e;
 

解决方案

包括早期往往迷失在某些类型的查询(即有额外的连接等)

要解决这个问题的方法是做查询,(然后只要你返回的实体,即选择e,而不是一个投影,即选择新{...}),您可以转换为的ObjectQuery并执行包括外绕:

  VAR更改为MyQuery =((从E在ContractContext.Equipments
              其中,e.Customers.ID == customer.ID
              从CCE在e.ContractEquipments
              其中,cce.Contracts.EndedOn> = DateTime.Today
              选择e)为的ObjectQuery<设备>)包括(制造商)。
 

这应该工作。

如果你对这个感兴趣的更多信息,请访问Tip 22 - 如何使包括真正包含

亚历

I have the following query:

var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
              where e.Customers.ID == customer.ID
              select e;

And everything works, I get my Equipments and it loads the Manufacturers table correctly (eagerly). But when I try to do the following many-to-many query:

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;

where "ContractEquipments" is a many-to-many lookup between "Equipments" and "Contracts", but when this query runs, the Manufacturers table no longer gets easily loaded. Any idea how to fix this without doing the following:

if (MyEntity.Manufacturers.IsLoaded == false) 
   MyEntity.ManufacturersReference.Load()

This project takes hours execute and I want to keep the number of database calls down.

EDIT #1:

I also tried this without success:

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;

解决方案

Early includes often get lost on some types of queries (i.e. with extra joins etc)

The way to get around this is to do the query, (and then so long as you are returning entities i.e. Select e rather than a projection i.e. Select new {...}) you can cast to ObjectQuery and do the include around the outside:

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

This should work.

If you are interested in more info on this, check out Tip 22 - How to make Include really Include

Alex

阅读全文

相关推荐

最新文章