Entity Framework creating LEFT OUTER JOINs when it should do INNER JOINs (multiple tables)

I have the following weird behavior:

public class Result { [Key] public int Id { get; set; } public int RuleId { get; set; } public int UserId { get; set; } [ForeignKey("RuleId")] public virtual Rule Rule { get; set; } [ForeignKey("UserId ")] public virtual User User { get; set; } } public class Rule { [Key] public int Id { get; set; } public string Name { get; set; } // More columns } public class User { [Key] public int Id { get; set; } public string Name { get; set; } // More columns } 

If I do this:

 ctx.Results.AsQueryable().Select(r => new { r.Rule.Name } ).ToList(); 

A query made in SQL Server makes an inner join in Rule:

 SELECT [Extent1].[RuleId] AS [RuleId], [Extent2].[Name] AS [Name] FROM [dbo].[result] AS [Extent1] INNER JOIN [dbo].[rule] AS [Extent2] ON [Extent1].[RuleId] = [Extent2].[Id] 

BUT if I do this:

 ctx.Results.AsQueryable().Select(r => new { rn = r.Rule.Name, un = r.User.Name } ).ToList(); 

A completed request executes an internal join rule, but an OUTER JOIN for the user.

 SELECT [Extent1].[UserId] AS [UserId], [Extent2].[Name] AS [Name], [Extent3].[Name] AS [Name1] FROM [dbo].[result] AS [Extent1] INNER JOIN [dbo].[rule] AS [Extent2] ON [Extent1].[RuleId] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[user] AS [Extent3] ON [Extent1].[UserId] = [Extent3].[Id] 

Is it really wrong?

+4
c # linq entity-framework
source share
1 answer

The answer was: An error in the structure of entities. Download the latest version and they are all internal.

+4
source share

All Articles