Repository navigation
Closed
Description
Steps to reproduce
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; }
}
Mapping on Country
builder.ToTable("Country");
builder.HasKey(pr => pr.CountryCode);
builder.HasMany(m => m.States).WithOne(i => i.Country).HasForeignKey(m => m.CountryCode);
Mapping on State
builder.ToTable("State");
builder.HasKey(pr => pr.StateCode);
builder.HasOne(m => m.Country).WithMany(m => m.States).HasForeignKey(m => m.CountryCode);
Now the 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();
The issue
When I run SQL Profiler on this query I get following
SELECT [i].[CountryName], [i].[CountryCode]
FROM [Country] AS [i]
WHERE [i].[CountryCode] = N'USA'
and
SELECT [x].[CountryCode], [x].[StateCode], [x].[StateName]
FROM [State] AS [x]
I understand the two queries are not combined as in previous versions of EntityFramework. However, here the query on State is just select all without any where condition from the Country it is looking for. It does gives me States from 'USA' but it is doing so in the memory, not the SQL server.
Now make the query to include about 5 countries, it gives me 5 of these queries without any condition for Country
SELECT [x].[CountryCode], [x].[StateCode], [x].[StateName]
FROM [State] AS [x]
Further technical details
EF Core version: 1.0.0-rc2-final
Operating system: Windows 10
SQL Server 2008
Visual Studio version: VS 2015 update 2
Framework Target: net461
Activity
rowanmiller commentedon Jun 13, 2016
@anpete @maumar is this already tracked by another issue?
maumar commentedon Jun 13, 2016
Related to #4007
ruchanb commentedon Jun 14, 2016
Is this a bug or work in progress?
rowanmiller commentedon Jun 15, 2016
Closing as a dupe
[-]Linq to SQL on relation[/-][+]Query: Generating unfiltered navigation property projections[/+]rowanmiller commentedon Jun 21, 2016
Reopening as we are going to track the lack of filtering issue separately from the n+1 optimization.
anpete commentedon Jun 21, 2016
cc @maumar
[-]Query: Generating unfiltered navigation property projections[/-][+]Query: Selecting entire table for navigation properties in projections[/+]gdoron commentedon Jun 21, 2016
Don't get me wrong, I really appreciate the team's hard work and all.
But EF might get the RTM title in couple of days, but it's not there yet.
A trivial query resulting in a full table retrieval, that's not RTM.
Many things have changed to the best in Microsoft in the last couple of years, including the open source and multi platform change, but "version 1 isn't reliable - wait for SP1" hasn't.
😢
11 remaining items
rowanmiller commentedon Jul 8, 2016
Realocating to 1.1.0 as the fix for this was pretty high risk.
maumar commentedon Jul 8, 2016
fixed in #6022, merged to dev in 0669372
gdoron commentedon Jul 9, 2016
@rowanmiller Thanks for the fix and the update!
Is there an estimation on when 1.1.0 will be released?
The roadmap hasn't been updated for a month.
ruchanb commentedon Sep 12, 2016
I used 1.1.0-alpha1-22107 and see no change in the query.
maumar commentedon Sep 20, 2016
@ruchanb I just run the following code:
on our nightly build and got this (expected) sql:
Is there any difference between the code you have and the one above?
ruchanb commentedon Sep 21, 2016
Hmm, I tried your code and the sql seems to be as you said. What about the no of queries on
Statethough? State is queried each time as the no of countries listed..Thanks.
maumar commentedon Sep 21, 2016
@ruchanb N+1 queries for top level collections is tracked here: #4007
this fix aimed to mitigate it somewhat by only pulling in relevant entities, rather than entire DbSets