Entity framework, code first. Child objects not populating when called

c# ef-code-first entity-framework

Question

I'm still learning how to use EF code. When I call my domain model objects in code, it doesn't seem to enable automatically "populating" their children.

Model:

public class Car
{
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required,MaxLength(10)]
    public string Registration { get; set; }

    [MaxLength(30)]
    public string Make { get; set; }

    [MaxLength(45)]
    public string Model { get; set; }

    [Required]
    public Coordinates Coordinates { get; set; }

    [Required]
    public Client Client { get; set; }                    
}

public class Coordinates
{
    [Key, ForeignKey("Car")]
    public int Id { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

    [Required]
    public Car Car { get; set; }
}

For instance, I merely dial:

public List<Car> Get()
{            
    var cars = _context.Cars.ToList();
    return cars;
}

And all of the things in my objectCars data from the database, but excludes theCoordinates . The data was appropriately produced by the database seed, but I can't get EF to reference it automatically.Coordinates , orClient in that case. However, I believe that if we can solve one, the other will follow.

What am I doing wrong, and have I got this all wrong?

1
10
6/1/2014 1:18:18 PM

Accepted Answer

Here, you have a few options to consider:

  • to instruct EF to Include() the relevant entities in order to eagerly load them. You may, for instance, loadCars such as theirCoordinates and Clients as in this:

    public List<Car> Get()
    {            
        var cars = _context.Cars
            .Include(car => car.Coordinates)
            .Include(car => car.Client)
            .ToList();
        return cars;
    }
    
  • to declare the navigation properties and lazy load associated entitiesvirtual directing EF to do so upon initial access. Make sure that lazy loading is not deactivated for your context in the following way:

    this.Configuration.LazyLoadingEnabled = false;

A succinct illustration would be as follows:

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!
}
  • Add linked entities explicitly to the context. The navigation characteristics will then be filled out for you by the context. resembles this:

    public List<Car> 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;
    }
    
29
6/1/2014 2:33:35 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow