1

I have 2 POCO classes like Category and Parent. In my conceptual model, I have a navigation property from Parent to Category, but not the other way around (from Category to Product).

I have been able to successfully create a Product and assign a Category to it and save the changes, like:

Product p = new Product();
p.Category = someCategory; 
context.SaveChanges();

However, when I load Products, Category is property NULL. Any advice?

Cheers, Mosh

Mosh
  • 5,944
  • 4
  • 39
  • 44

2 Answers2

2

Try eager loading the Category when you retrieve the Product:

var product = ctx.Products.Single(x => x.ProductId == 1).Include("Category");
RPM1984
  • 72,246
  • 58
  • 225
  • 350
0

In addition to RPM's answer, I asked a related question a while ago on getting compile-time checking for the stuff you Include():

Entity Framework .Include() with compile time checking?

Community
  • 1
  • 1
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158