使用Include()时,如何避免在EF中生成大量SQL查询

b3n*_*b3n 12 entity-framework entity-framework-4

我正在使用EF(DLL版本为4.4)来查询数据库.该数据库包含几个包含课程信息的表.当看看实际发送给db的内容时,我看到了一个巨大的,近1300行的SQL查询(由于它的大小,我不会在这里粘贴).我在上下文中运行的查询如下所示:

entities.Plans
  .Include("program")
  .Include("program.offers")
  .Include("program.fees")
  .Include("program.intakes")
  .Include("program.requirements")
  .Include("program.codes")
  .Include("focuses")
  .Include("codes")
  .Include("exceptions")
  .Include("requirements")
where plans.Code == planCode
select plans).SingleOrDefault(); 
Run Code Online (Sandbox Code Playgroud)

我想避免在从每个相关表中收集信息时回到服务器,但是如此大的查询我想知道是否有更好的方法来做到这一点?

谢谢.

Tre*_*ent 0

通常可以.Include()在 where 子句之后添加。这意味着您只是提取与您想要的信息相匹配的信息,看看这是否会减少您的查询。