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 开发者_Go百科load Products, Category is property NULL. Any advice?
Cheers, Mosh
Try eager loading the Category when you retrieve the Product:
var product = ctx.Products.Single(x => x.ProductId == 1).Include("Category");
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?
精彩评论