I have an Entity Framework 4 model with two objects containing many-to-many relationships, so 3 tables, [Q], [P] and [Q2P] is a scrolling table. Running code, for example:
context.Q.Include("P");
Results for a long time (I waited like 5 minutes and then interrupted it). Then I checked the generated SQL and found this:
SELECT * FROM ( SELECT * FROM [Q] AS [Extent1] LEFT OUTER JOIN (SELECT *, CASE WHEN ([Join1].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2] FROM [Q_P] AS [Extent2] INNER JOIN [P] AS [Extent3] ON [Extent3].[Id] = [Extent2].[Id] ) AS [Join1] ON [Extent1].[Id] = [Join1].[Id] ) AS [Project1] ORDER BY [Project1].[Id] ASC, [Project1].[C2] ASC
I canβt hide my surprise, WTF it? Plain many-to-many SQL query
select * from [q2p] join [q] on qId=q.Id join [p] on pId=p.Id
It is executed in less than 1 ms, and the EF request is executed forever.
source share