Wednesday, December 17, 2014

Entity framework load children objects

The code below would fetch a list of all the categories through entity framework and would include the Products associated with each category and Brands associated with each product.


public class Category
{
  public ICollection<Product> Products;
}

public class Product
{
  public ICollection<Brand> Brands;
}

public class Brand
{
   public string Name{get;set;}
}

using (var db = new DatabaseContext())
{
   var schema = db.Categories
     .Include("Products")
     .Include("Products.Brands").ToList();
}

No comments: