Entity framework not loading relationship even when using include
I have this code:
Category selectedCategory = (from c in DB.Category.Include("SubCategory")
join a in DB.Accessory on c.AccCatUID equals a.Category.AccCatUID
where a.AccUID == currentAccessory.AccUID
select c).FirstOrDefault();
It works fine, selectedCategory gets populated as expected. BUT selectedCategory has a child table 'SubCategory' which does not get loaded even though there is the 开发者_JAVA百科include there. It is not loaded until I do this:
selectedCategory.SubCategory.Load();
Why do I have to call load explicitly in order to load the child table?
EDIT: Using .net 3.5 VS2008
The include is being dropped because it is associated with the first ObjectQuery and not the result set that is coming back.
I think you just need to rework your query? Unless I am missing something you really want the below, i didn't see anywhere you use the accessory join.
Category selectedCategory = (
from c in DB.Category.Include("SubCategory")
where c.AccCatUID == currentAccessory.AccUID
select c
).FirstOrDefault();
or from the otherside
var fromAccessoryCategory =
(from a in DB.Accessory.Include("Category.SubCategory")
where a.AccUID == currentAccessory.AccUID
select a.Category).FirstOrDefault();
精彩评论