EF4 Lazy Loading - How do I force a load?
I have the following code:
public IEnumerable<RMAInfo> GetAllRMAsByReseller(string resellerID)
{
开发者_开发知识库 using (RMAEntities context = new RMAEntities())
{
IEnumerable<RMAInfo> ret = from r in GetRMAInfos_Internal(context)
where r.ResellerID.ToUpper().Trim() == resellerID
select r;
return ret;
}
}
When I try and access the return value, I get the expected error of:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I know this is happening because of lazy loading, and it's trying to load now that the context has been closed (via the using going out of scope).
My question is how do I force a load before I exit the method?
I originally did it like
return ret.ToList<RMAInfo>();
This worked fine, but I'm not sure if it's the correct or best way to do it?
query.ToList();
is the correct way to force load the results. How else would you force evaluation of linq query?
That is perfectly fine and accepted.
精彩评论