Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too Many Left Outer Joins in Entity Framework 4? [duplicate]

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
like image 688
Adam Rackis Avatar asked May 26 '10 21:05

Adam Rackis


2 Answers

That extra outer join does seem quite superfluous. I think it's best to contact the entity framework design team. They may know if it's a bug and see if it something that needs to be resolved in a next version. You can contact them at Link

like image 177
René Wolferink Avatar answered Nov 15 '22 18:11

René Wolferink


It may be a bug, but it seems like such a simple example that it is strange that the bug has not been caught and fixed.

Could you check your EF model.

Has the BestSeller table been added twice, or is there a duplication in the relationship between the tables.

like image 27
Shiraz Bhaiji Avatar answered Nov 15 '22 18:11

Shiraz Bhaiji