Mosh
Mosh

Reputation: 6074

Related objects are not loaded in Entity Framework

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

Upvotes: 1

Views: 669

Answers (2)

Mike Chamberlain
Mike Chamberlain

Reputation: 42530

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?

Upvotes: 0

RPM1984
RPM1984

Reputation: 73123

Try eager loading the Category when you retrieve the Product:

var product = ctx.Products.Single(x => x.ProductId == 1).Include("Category");

Upvotes: 2

Related Questions