我正在考虑将 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();
}
}
但是当它尝试执行第二个查询时它会被处理掉。你能告诉我哪里出错了吗?