0
我有三個實體類。Entity Framework查詢ManyToMany關係時遇到的問題
public partial class Person
{
public Person()
{
this.Locations = new HashSet<Location>();
}
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
public partial class Location
{
public Location()
{
this.People = new HashSet<Person>();
}
public int LocationID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public int CityID { get; set; }
public virtual City City { get; set; }
public virtual ICollection<Person> People { get; set; }
}
public partial class City
{
public City()
{
this.Locations = new HashSet<Location>();
}
public int CityID { get; set; }
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
我想查詢我的實體並獲取給定人員的所有位置。 到目前爲止,我有這種方法。
public IQueryable<Person> GetLocationsForPerson(int id)
{
return context.People
.Include(p => p.Locations)
.Where(p => p.PersonID == id);
}
這是工作正常。問題是,我想獲得每個位置的城市的名稱了。當我從Location表中獲取cityID時,Location實體的City屬性返回null。爲什麼是空?以及如何修改我的查詢以獲取城市名稱?任何提示將不勝感激。