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
。另外,不应该将这两个查询结合起来吗?
这里有什么问题?