I have three Entity classes.
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; }
}
I am trying to query my entities and get all the locations of a given person. So far i have this method.
public IQueryable<Person> GetLocationsForPerson(int id)
{
return context.People
.Include(p => p.Locations)
.Where(p => p.PersonID == id);
}
which is working fine.The problem is that i want to get the name of the city for each location too. while i am getting the cityID from Location table, the City property of Location entity is returning null. Why is that null? and how can i modify my query to get the City Name? Any hint would be greatly appreciated.