views:

103

answers:

1

I have Tasks table with two navigation properties - VersionReported and VersionResolved, both stored in table Versions. When I try to get Task list with included both properties, I get too many joins in SQL (this is only part of sql from profiler):

LEFT OUTER JOIN [dbo].[Versions] AS [Extent4] ON [Extent1].[IDVersionReported] = [Extent4].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent5] ON [Extent1].[IDVersionReported] = [Extent5].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent6] ON [Extent1].[IDVersionReported] = [Extent6].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent7] ON [Extent1].[IDVersionReported] = [Extent7].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent8] ON [Extent1].[IDVersionResolved] = [Extent8].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent9] ON [Extent1].[IDVersionResolved] = [Extent9].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent10] ON [Extent1].[IDVersionResolved] = [Extent10].[ID]
LEFT OUTER JOIN [dbo].[Versions] AS [Extent11] ON [Extent1].[IDVersionResolved] = [Extent11].[ID]

Extent1 is Tasks table. I know that EntityFramework has problem when two or more navigation properties lead to the same table, but did anyone find a solution?

+1  A: 

I joined an EF project about 2 months ago, and we noticed this problem too.

I guess the simplest (and best performing) solution is to create a view which does all the JOIN magic, and map that view in EF.

On the other hand, requiring views for every performance problem is probably not what we expected when we started with EF.

devio