实体框架中的外部连接太多4? [英] Too Many Left Outer Joins in Entity Framework 4?

查看:103
本文介绍了实体框架中的外部连接太多4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品实体,它有0或1个BestSeller实体。由于某些原因,我说:

  db.Products.OrderBy(p => p.BestSeller.rating).ToList( ); 

我得到的SQL有一个额外外连接(下面)。如果我添加第二个0或1关系船,并且由两者订购,那么我将获得外连接。似乎每个这样的实体都生产2个外连接而不是一个。 LINQ to SQL的行为与您预期的完全一样,没有额外的加入。



有没有人经历过这个,或者知道如何解决它?

  SELECT 
[Extent1]。[id] AS [id],
[Extent1]。[ProductName] AS [ProductName]
FROM [dbo]。[产品] AS [Extent1]
LEFT OUTER JOIN [dbo]。[BestSeller] AS [Extent2] ON [Extent1]。[id] = [Extent2]。[id]
LEFT OUTER JOIN [dbo]。[BestSeller] AS [Extent3] ON [Extent2]。[id] = [Extent3]。[id]
ORDER BY [Extent3]。[rating] ASC


解决方案

我问Julie Lerman在这里回答了一个类似的问题: p>

请参阅简单Linq查询对同一表格重复加入?


I have a product entity, which has 0 or 1 "BestSeller" entities. For some reason when I say:

db.Products.OrderBy(p => p.BestSeller.rating).ToList();

the SQL I get has an "extra" outer join (below). And if I add on a second 0 or 1 relation ship, and order by both, then I get 4 outer joins. It seems like each such entity is producing 2 outer joins rather than one. LINQ to SQL behaves exactly as you'd expect, with no extra join.

Has anyone else experienced this, or know how to fix it?

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[ProductName] AS [ProductName]
FROM   [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN [dbo].[BestSeller] AS [Extent2] ON [Extent1].[id] = [Extent2].[id]
LEFT OUTER JOIN [dbo].[BestSeller] AS [Extent3] ON [Extent2].[id] = [Extent3].[id]
ORDER BY [Extent3].[rating] ASC

解决方案

I asked Julie Lerman to answer a similar question here:

Please see Simple Linq query has duplicated join against same table?

这篇关于实体框架中的外部连接太多4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆