I am using Entity Framework Core 1.0.0 RC2 final version.
I have 2 database models Country and State
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
public virtual ICollection<State> States { get; set; }
}
public class State
{
public string StateCode { get; set; }
public string StateName { get; set; }
public string CountryCode { get; set; }
public Country Country { get; set; }
}
The mapping from State is done as below for Country:
builder.ToTable("Country");
builder.HasKey(pr => pr.CountryCode);
builder.HasMany(m => m.States).WithOne(i => i.Country).HasForeignKey(m => m.CountryCode);
for State:
builder.ToTable("State");
builder.HasKey(pr => pr.StateCode);
builder.HasOne(m => m.Country).WithMany(m => m.States).HasForeignKey(m => m.CountryCode);
Now when I run the following linq query.
var query = _context.Countries
.Where(i => i.CountryCode == "USA")
.Select(m => new
{
m.CountryName,
m.CountryCode,
States = m.States.Select(x => new
{
x.StateCode,
x.StateName,
x.CountryCode
})
}).AsQueryable();
return query.ToList();
When I run SQL Server profiler, it shows:
SELECT [i].[CountryName], [i].[CountryCode]
FROM [Country] AS [i]
WHERE [i].[CountryCode] = N'USA'
SELECT [x].[CountryCode], [x].[StateCode], [x].[StateName]
FROM [State] AS [x]
The State query doesn't have any WHERE clause to check with the CountryCode. Also, shouldn't the two queries be combined?
What's the issue here?