2
我正在使用實體框架核心1.0.0 RC2最終版本。實體框架核心1沒有做關係查詢
我有2個數據庫模型Country
和State
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
。另外,不應該將這兩個查詢結合起來嗎?
這裏有什麼問題?