实体框架使用包含和order bys生成的低效查询

时间:2011-12-17 04:49:51

标签: c# sql-server entity-framework

1)我遇到的第一个问题是,如果你做了一个include,然后由生成的SQL生成一个内部联接和一个外部联接

var query = from l in Lead.Include("Contact")
orderby l.Contact.FirstName
select l;

在同一个表中生成以下内连接和外连接

 INNER JOIN [dbo].[Contact] AS [Extent2] 
      ON [Extent1].[ContactId] = [Extent2].[ContactId]
 LEFT OUTER JOIN [dbo].[Contact] AS [Extent3] 
      ON [Extent1].[ContactId] = [Extent3]. [ContactId]
ORDER BY [Extent2].[FirstName] ASC

这使得查询效率稍低[/ p>

2)如果我做多个包含它总是把第二个作为外连接,所以像

 Lead.Include("OneToOne").Include("OtherOneToOne") <- in this scenario     
                                                      OtherOneToOne is an outer 
                                                     join and OneToOne is an inner 
                                                     join
 Lead.Include("OtherOneToOne").Include("OneToOne") <- in this scenario 
                                                      OneToOne is an outer join 
                                                      and OtherOneToOne is an 
                                                      inner join

它是如何运作的?

我发现了另一篇帖子,有人在谈论这个问题,他们说这是在6月CTP版本中修复的 http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx

但是我安装并设置了要使用它仍然无效..

好吧,它不会让我回答我自己的问题

所以

修改 好吧,我设置了一个独立的测试,发现http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx似乎解决了这些

但由于我使用的是RIA,因为6月ctp不支持RIA,所以运气不好: - /

1 个答案:

答案 0 :(得分:0)

解决方案是自己做包括:

var query = from l in Lead
select new { l, l.Contact } into row
orderby row.Contact.FirstName
select row;