【发布时间】:2016-06-10 06:13:23
【问题描述】:
我正在使用 Entity Framework Core 1.0.0 RC2 最终版。
我有 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);
现在当我运行以下 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 分析器时,它显示:
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 一起检查。另外,这两个查询不应该合并吗?
这里有什么问题?
【问题讨论】: