开发者

Related objects are not loaded in Entity Framework

开发者 https://www.devze.com 2023-02-04 09:27 出处:网络
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 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消