Too Many Left Outer Joins in Entity Framework 4?

Posted by Adam on Stack Overflow See other posts from Stack Overflow or by Adam
Published on 2010-05-26T21:08:59Z Indexed on 2010/05/26 21:11 UTC
Read the original article Hit count: 223

Filed under:

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

© Stack Overflow or respective owner

Related posts about entity-framework-4