I\'m getting to grips with EF code first. My domain model design doesn\'t seem to support the auto \'populating\' child of objects when I call them in code.
Mod
You have a couple of choices here:
To eagerly load related entities by telling EF to Include() them. For example you can load Cars including their Coordinates and Clients like this:
public List Get()
{
var cars = _context.Cars
.Include(car => car.Coordinates)
.Include(car => car.Client)
.ToList();
return cars;
}
To lazy load related entities by declaring the navigation properties virtual thus telling EF to load them upon first access. Make sure you don't have disabled lazy loading for your context like this:
this.Configuration.LazyLoadingEnabled = false;
A short example would look like this:
public class Car
{
// ... the other properties like in your class definition above
public virtual Coordinates Coordinates { get; set;}
}
public void Get()
{
var cars = _context.Cars.ToList();
var coordinates = cars.First().Coordinates; // EF loads the Coordinates of the first car NOW!
}
Explicitly load related entities to the context. The context will then populate the navigation properties for you. Looks like this:
public List Get()
{
// get all cars
var cars = _context.Cars.ToList();
// get all coordinates: the context will populate the Coordinates
// property on the cars loaded above
var coordinates = _context.Coordinates.ToList();
return cars;
}