2
我正在考虑将ObjectContext置于HttpContext.Current中,以便相同请求中的所有逻辑都可以访问它,而无需每次都打开/销毁。 在ObjectContextManager类中,我创建了这个。执行1个查询后处理的每个http请求的ObjectContext实例
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();
}
}
但它会在尝试执行第二个查询时处理。 你能告诉我我哪里出错了吗?
你在执行第一个查询,比如'using(JEntities jEntities = ObjectContextManager.CurrentObjectContext){var entities = jEntities.Foo;}'? – Eranga 2011-05-30 08:43:47