2016-06-10 64 views
2

我正在使用实体框架核心1.0.0 RC2最终版本。实体框架核心1没有做关系查询

我有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); 

现在,当我运行以下l inq查询。

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

这里有什么问题?

回答

0

不幸的是,他加载了数据库中的所有状态并将其过滤到内存中。 也许你可以用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) 
      ) 
     ) 
    ) 
)