简体   繁体   English

EF6触发“包含”新查询吗?

[英]EF6 triggers new query on “Include”?

I am using EF6 and I noticed that when I "include" a child table in a query, EF triggers a new query for each one of the parent rows...is that ok? 我正在使用EF6,我注意到当我在查询中“包括”一个子表时,EF为每个父行触发一个新查询...可以吗? is there a way to avoid it and make it bring all the information with the main query only? 有没有一种方法可以避免这种情况,并使所有信息仅包含在主查询中?

Here are my entities (not the exact code): 这是我的实体(不是确切的代码):

public class Contractor
{
   public int id { get; set;}
   public IEnumerable<ContractorEmployee> Employees;
}

public class ContractorEmployee
{
   public int id { get; set;}
   public int contractorId { get; set;}
}

And this is the query: 这是查询:

var fullContractors = dbContext.Contractors.Include("ContractorEmployee");

so if the fullContractors query retrieves 5 contractors, I see 5 extra queries in SQL bringing the contractor employees of each one of them. 因此,如果fullContractors查询检索到5个承包商,则我在SQL中看到5个额外的查询,这些查询使每个承包商都雇用了承包商。

Any way to avoid this and bring it all in the first "SELECT"??? 任何避免这种情况的方式都可以放在第一个“ SELECT”中?

Include method takes the name of the parameter as string it is recommended to use overloaded version of Include that takes Expression as parameter. Include方法将参数名称作为字符串,建议使用以Expression为参数的Include的重载版本。 You can have look at the overloaded version of extension method here . 您可以在此处查看扩展方法的重载版本。

So do it Either 也可以

var fullContractors = dbContext.Contractors.Include("Employees");

or go with Expression version like this: 或使用像这样的Expression版本:

var fullContractors = dbContext.Contractors.Include(d => d.Employees);

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

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