当我访问所有城市时,我的代码就像这样。实体框架包含父实体
public IQueryable<City> GetAll()
{
var result = from s in this.Context.Cities.Include("States.Countries") select s;
return result;
}
这工作正常,包括状态和countires。我想通过国家ID获取城市,下面是我的代码。在下面的代码中,我想包含每个城市的States.Countires。我怎样才能做到这一点 ?
public IEnumerable<City> GetByCountriesId(int Id)
{
var result = from s in this.Context.Countries
join a in this.Context.States on s.Id equals a.Country_Id
join b in this.Context.Cities on a.Id equals b.States_Id
where s.Id == Id
select b;
return result;
}
'this.Context.Cities.Include( “State.Country”)。如果“City”和“State”具有适当的导航属性,那么(c => c.State.Country.Id == Id)'就足够了。 –
你使用什么版本的EF?我问,因为包含你使用的字符串参数是旧的,你应该使用表达式方法来支持编译时。例如:'.Include(x => x.States)' –