实体框架中的外部连接太多4? [英] Too Many Left Outer Joins in Entity Framework 4?
问题描述
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>
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屋!