Simple Linq query has duplicated join against same table?

Solution 1:

Thank you for reporting this issue. Yes, it is not expected to see both joins. This is a bug that has been fixed in the current (not yet released) bits. In the current bits the same query produces:

SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[ContactId] AS [ContactId],
...
[Extent2].[Id] AS [Id1],
..
[Extent2].[FirstName] AS [FirstName],
[Extent2].[LastName] AS [LastName]
FROM  [dbo].[Address] AS [Extent1]
INNER JOIN [dbo].[Contact] AS [Extent2] ON [Extent1].[ContactId] = [Extent2].[Id] ORDER BY [Extent2].[LastName] ASC, [Extent2].[FirstName] ASC

I bit of playing shows that even on 4.0 this only happens when the relationship is 1:many, 0..1:many seems fine.

Thanks,

Kati Iceva
Entity Framework Developer
Microsoft

Solution 2:

Hobbes, I don't know why you are getting both the inner and outer join. Include does weird stuff to queries. I've put your question to the team to see if this is expected (as bad as it is) and if there's a way to alleviate it. The query in question is just an example to demonstrate that you can use entity ref navigation properties in a query if you get in a bind.