SelectMany with DefaultIfEmpty() generates unexpected INNER JOIN
In EF Core same code with SelectMany and DefaultIfEmpty() generates INNER JOIN and brings unexpected result, while in EF 6 it works perfectly fine with LEFT JOIN
Steps to reproduce
Let's assume we've simple one->one->many relation with following tables structure:
public class City
{
public string Name { get; set; }
[Column("DtaCentralSchoolId")]
[ForeignKey("MyCentralSchool")]
public int? CentralSchoolId { get; set; }
public CentralSchool MyCentralSchool { get; set; }
}
public class CentralSchool
{
public string Name { get; set; }
[InverseProperty("MyCentralSchool")]
public virtual IList<Student> MyStudents { get; set; }
}
public class Student
{
public string Name { get; set; }
[Column("DtaCentralSchoolId")]
[ForeignKey("MyCentralSchool")]
public int? CentralSchoolId { get; set; }
public CentralSchool MyCentralSchool { get; set; }
}
And trying to run the following query:
var result = await dbContext.Set<City>()
.AsNoTracking()
.SelectMany(x => x.MyCentralSchool.MyStudents.DefaultIfEmpty(), (c, s) => new {City = c, Student = s})
.Where(x => x.Student == null || !x.Student.IsDeleted && x.Student.MyStoreId == storeId)
.FirstOrDefaultAsync();
So for some reason for CentralSchool INNER JOIN is getting to be generated, while for Student there is LEFT JOIN, which is totally fine, as far as DefaultIfEmpty() is used. Actually I expect LEFT JOIN for CentralSchool as well, so when there is no CentralSchool some rows will be in the result anyway, how can I achieve that in current construction without manually rewriting ugly query and forcing LEFT JOIN to appear?
Further technical details
EF Core version: 2.1.2 Database Provider: Confirmed both on Microsoft.EntityFrameworkCore.SqlServer and Devart.Data.MySql.EFCore
Please check this stackoverflow question and answer for reference: https://stackoverflow.com/questions/52608672/ef-core-2-1-force-selectmany-to-produce-left-join
Triage: we will look at this for 2.2, but will punt to 3.0 if it gets complicated and/or risky.
fixed in 172d5e68db74043d649c070300e3a78757e33da1