-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: Related tables sometimes result in incorrect type of join #6844
Comments
Further ExplanationIf we use the code below than the sql output is what we expect.
SQL Output
Note that the Suspended table is correctly added to the statement as a LEFT JOIN.
SQL Output
What we see here is that the Suspended table is added to the sql as INNER JOIN now. And if Suspended table has no record for the user the statement will not return record. ConclusionReferencing a related table (in a where clause or select) changes the LEFT JOIN to INNER JOIN and results no record if the referenced table has no record related to the parent table. This really kills the minimal approach. |
This works in the current bits. We produce the following sql: SELECT TOP(2) [x].[Id], [x].[Email], [x.Role].[Id], [x.Role].[UserId], [x.Suspended].[Id], [x.Suspended].[UserId]
FROM [Users] AS [x]
LEFT JOIN [Role] AS [x.Role] ON [x].[Id] = [x.Role].[UserId]
LEFT JOIN [Suspended] AS [x.Suspended] ON [x].[Id] = [x.Suspended].[UserId]
WHERE ([x].[Email] = @__username_0) AND [x.Suspended].[Id] IS NULL |
Steps to reproduce
Using EF and LINQ to query two tables that are related 1 to 1 or non fails since the sql that is generated by EF is invalid. The UserId is set as PK for both tables "User" and "Suspended". The relation is on UserId field (FK_Suspended_User). Here is a link to the complete description in stack overflow.

Here is the code:
The output in sql is:
The issue
As you can see the "Suspended" table is joined twice INNER and LEFT. As a result the record is not selected if x.Suspended is null. Also there is no WHERE clause.
Further technical details
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1"
Operating system: Windows 10
Visual Studio version: VS 2015
Project setup: Asp.Net Core Web API
The text was updated successfully, but these errors were encountered: