Entity framework core 实体框架核心1未执行关系查询

Entity framework core 实体框架核心1未执行关系查询,entity-framework-core,Entity Framework Core,我使用的是实体框架核心1.0.0 RC2最终版本 我有两个数据库模型国家和州 public class Country { public string CountryCode { get; set; } public string CountryName { get; set; } public virtual ICollection<State> States { get; set; } } public class State {

我使用的是实体框架核心1.0.0 RC2最终版本

我有两个数据库模型
国家

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; }
}
对于
状态

builder.ToTable("State");
builder.HasKey(pr => pr.StateCode);
builder.HasOne(m => m.Country).WithMany(m => m.States).HasForeignKey(m => m.CountryCode);
现在,当我运行以下linq查询时

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();
当我运行SQL Server profiler时,它显示:

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]
State
查询没有任何要与
CountryCode
检查的
WHERE
子句。另外,这两个查询不应该合并吗


这里有什么问题?

不幸的是,他从数据库中加载所有状态,并在内存中过滤它们。 也许您可以使用EF6测试这种行为,并在中打开一个问题

(QueryContext QueryContext)=>IEnumerable\u选择(
来源:IEnumerable\u ShapedQuery(
queryContext:queryContext,
shaperCommandContext:选择表达式:
选择[i].[CountryName],[i].[CountryCode]
来自[国家]作为[我]
其中[i].[CountryCode]=N'USA'
, 
shaper:ValueBufferShaper
)
, 
选择器:(ValueBuffer i)=>新f_u匿名类型2(
(字符串)对象i.get_项(0),
(字符串)对象i.get_项(1),
IEnumerable\u Select(
来源:IEnumerable\u Where(
来源:IEnumerable\u ShapedQuery(
queryContext:queryContext,
shaperCommandContext:选择表达式:
选择[x].[CountryCode],[x].[StateCode],[x].[StateName]
来自[州]作为[x]
, 
shaper:ValueBufferShaper
)
, 
谓词:(ValueBuffer x)=>(string)对象i.get\u项(1)==(string)对象x.get\u项(0)
)
, 
选择器:(ValueBuffer x)=>新f_u匿名类型3(
(字符串)对象x.get_项(1),
(字符串)对象x.get_项(2),
(字符串)对象x.get_项(0)
)
)
)
)
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]
(QueryContext queryContext) => IEnumerable<<>f__AnonymousType2<string, string, IEnumerable<<>f__AnonymousType3<string, string, string>>>> _Select(
    source: IEnumerable<ValueBuffer> _ShapedQuery(
        queryContext: queryContext, 
        shaperCommandContext: SelectExpression: 
            SELECT [i].[CountryName], [i].[CountryCode]
            FROM [Countrys] AS [i]
            WHERE [i].[CountryCode] = N'USA'
        , 
        shaper: ValueBufferShaper
    )
    , 
    selector: (ValueBuffer i) => new <>f__AnonymousType2<string, string, IEnumerable<<>f__AnonymousType3<string, string, string>>>(
        (string) object i.get_Item(0), 
        (string) object i.get_Item(1), 
        IEnumerable<<>f__AnonymousType3<string, string, string>> _Select(
            source: IEnumerable<ValueBuffer> _Where(
                source: IEnumerable<ValueBuffer> _ShapedQuery(
                    queryContext: queryContext, 
                    shaperCommandContext: SelectExpression: 
                        SELECT [x].[CountryCode], [x].[StateCode], [x].[StateName]
                        FROM [States] AS [x]
                    , 
                    shaper: ValueBufferShaper
                )
                , 
                predicate: (ValueBuffer x) => (string) object i.get_Item(1) == (string) object x.get_Item(0)
            )
            , 
            selector: (ValueBuffer x) => new <>f__AnonymousType3<string, string, string>(
                (string) object x.get_Item(1), 
                (string) object x.get_Item(2), 
                (string) object x.get_Item(0)
            )
        )
    )
)