Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code first Eager loading problem

I have two entities in 1:n relationship: Category and Product.

public class Category 
{
   public int CategoryID { get; set; }
   public string CategoryName { get; set; }

   public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
   public int ProductID { get; set; }
   public string ProductName { get; set; }

  public virtual Product { get; set; }
}

public class context : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

I want to load products in every category by Eager loading.

context.Categories.Include(c=>c.Products)

but Include do not load any navigation property. it accept only one parameter called "path" typed string.

like image 799
Ghooti Farangi Avatar asked Mar 08 '26 00:03

Ghooti Farangi


1 Answers

Are you missing a using? VS 2010 is a bit dumb on this one and you often have to explicitly put in:

using System.Data.Entity 

..to get the lambda include option available.

It won't prompt you to add it as it already has a string based definition for Include available under

System.Linq

.Include(x => x.MyObject) is actually a new extension method for the existing linq method.

like image 104
Gats Avatar answered Mar 10 '26 15:03

Gats



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!