2016-06-10 67 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) 
      ) 
     ) 
    ) 
)