2

我正在考虑将 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();
  }
}

但是当它尝试执行第二个查询时它会被处理掉。你能告诉我哪里出错了吗?

4

1 回答 1

2

处置?您的代码与处置无关。如果您获得了已处置的上下文,则意味着您很可能将上下文检索包含在其中using并且您自己处置了该实例。

于 2011-05-30T08:39:56.553 回答