Entity Framework核心1没有做关系查询

Entity Framework core 1 not doing relation query

我正在使用 Entity Framework Core 1.0.0 RC2 最终版。

我有 2 个数据库模型 CountryState

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; }
}

State 的映射如下为 Country 完成:

builder.ToTable("Country");
builder.HasKey(pr => pr.CountryCode);
builder.HasMany(m => m.States).WithOne(i => i.Country).HasForeignKey(m => m.CountryCode);

对于 State:

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 服务器分析器时,它显示:

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

这里有什么问题?

不幸的是,他从数据库中加载所有状态并在内存中过滤它们。 也许您可以使用 EF6 测试此行为并在 https://github.com/aspnet/EntityFramework

中打开一个问题
(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)
            )
        )
    )
)