EF4 延迟加载 - 如何强制加载?
我有以下代码:
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;
}
}
当我尝试访问返回值时,我得到了预期的错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
我知道这是由于延迟加载而发生的,并且它正在尝试加载现在上下文已关闭的情况(通过使用 outing范围)。
我的问题是如何在退出该方法之前强制加载?
我最初是这样做的,
return ret.ToList<RMAInfo>();
这工作得很好,但我不确定这是否是正确或最好的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
query.ToList();
是强制加载结果的正确方法。您还如何强制评估 linq 查询?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.