开发者

EF4 Lazy Loading - How do I force a load?

开发者 https://www.devze.com 2023-04-06 13:15 出处:网络
I have the following code: public IEnumerable<RMAInfo> GetAllRMAsByReseller(string resellerID) { 开发者_开发知识库using (RMAEntities context = new RMAEntities())

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.

0

精彩评论

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

关注公众号