每个 http 请求的 ObjectContext 实例在执行 1 次查询后释放
我正在考虑将 ObjectContext 放入 HttpContext.Current 中,以便同一请求中的所有逻辑都可以访问它,而不必每次都打开/销毁。 在 ObjectContextManager 类中我创建了这个。
get {
string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
if (!HttpContext.Current.Items.Contains(ocKey))
HttpContext.Current.Items.Add(ocKey, new JEntities());
return HttpContext.Current.Items[ocKey] as JEntities;
}
然后每次我根据当前请求执行查询时都会调用此静态属性。
public static JEntities CurrentObjectContext {
get {
if (ObjectContextManager == null)
InstantiateObjectContextManager();
return ObjectContextManager.ObjectContext;
//return new JobsEntities();
}
}
但当它尝试执行第二个查询时,它会被释放。 你能告诉我我哪里错了吗?
I'm considering putting the ObjectContext inside HttpContext.Current so that all logic in the same request can access to it without having to open/destroy each time.
In ObjectContextManager class i created this.
get {
string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
if (!HttpContext.Current.Items.Contains(ocKey))
HttpContext.Current.Items.Add(ocKey, new JEntities());
return HttpContext.Current.Items[ocKey] as JEntities;
}
and then I call this static property every time i execute a query on current request.
public static JEntities CurrentObjectContext {
get {
if (ObjectContextManager == null)
InstantiateObjectContextManager();
return ObjectContextManager.ObjectContext;
//return new JobsEntities();
}
}
But it gets disposed when it tries to execute second query.
Can you tell me where i went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处置?您的代码与处置无关。如果您获得已处置的上下文,则意味着您很可能将上下文检索包含在
using
中,并且您自己处置了该实例。Disposed? Your code has nothing to do with disposing. If you get disposed context it means you most probably enclosed the context retrieval into
using
and you disposed the instance yourselves.