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; }
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?
c # linq entity-framework
Andrew White
source share