I have the following models in EF Code First:
public class A
{
public int Id { get; set; }
public virtual B { get; set; }
}
public class B
{
public int Id { get; set; }
public virtual A { get; set; }
}
I have defined the relationship as follows:
modelBuilder.Entity<A>().HasKey(entity => entity.Id);
modelBuilder.Entity<B>().HasKey(entity => entity.Id);
modelBuilder.Entity<A>()
.HasOptional(entity => entity.B)
.WithRequired(entity => entity.A);
When I write the following query:
var a = db.AItems.Include("B");
The query that is produced is as follows:
SELECT
[Extent1].[Id] AS [Id],
[Extent3].[Id] AS [Id1]
FROM [dbo].[As] AS [Extent1]
LEFT OUTER JOIN [dbo].[Bs] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Bs] AS [Extent3] ON [Extent2].[Id] = [Extent3].[Id]
Why does Entity Framework have an additonal (useless) left join for this type of relationship?