weird one. (Probably not weird, at all)
I have 3 objects, Employee, Rota and Department.
public class Employee
{
public int Id { get; set; }
publ
I have theory. The left outer join goes away when you turn off change tracking. Also Employee and Rota have references to Department.
So my theory is that with change tracking on the Entity Framework tries to load all entities with a reference to the department in case it has to cascade an update to department.
In other words it thinks that a change to a Department could cause a change to the Employee or Rota referencing the department so it loads everything just in case.
The reason is incorrect mapping. It looks correct but it is not. Use these instead:
internal class EmployeeMapping : EntityTypeConfiguration<Employee>
{
public EmployeeMapping()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("UserId");
HasRequired<Department>(a => a.Department).WithMany()
.Map(a => a.MapKey("DepartmentId"));
}
}
internal class RotaMapping : EntityTypeConfiguration<Rota>
{
public RotaMapping()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("RotaId");
HasOptional<Employee>(a => a.Employee).WithMany()
.Map(a => a.MapKey("EmployeeId"));
HasOptional<Department>(a => a.Department).WithMany()
.Map(a => a.MapKey("DepartmentId"));
}
}
Your mapping is correctly interpreted when creating database and database looks correct but EF thinks that you map all relations as one-to-one. That confuse EF and it will generate queries used for one-to-one to create internal entity references. These left joins are necessary for one-to-one relation when you tell EF that dependent entities are optional - EF doesn't know if they exist unless it loads their keys.
I am by no means an expert and I am just guessing here. But could you try the query the way entity framework is doing it and then try it the way you would ideally like to see the query and post the time differentials. Maybe the entity framework is on to something here, but I doubt it.