The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
该对象上下文实例已被释放,不能用于需要连接的操作。
shared context per request模式,缩短Entity实例的存在时间和降低Entity实例的共享性,并考虑性能,因为Entity需要手动Dispose。
public override void Dispose() { string objectContextKey = "MIP_" + HttpContext.Current.GetHashCode().ToString("x"); if (HttpContext.Current.Items.Contains(objectContextKey)) { SPMIPEntities ctx = HttpContext.Current.Items[objectContextKey] as SPMIPEntities; if (ctx != null) { ctx.Dispose(); HttpContext.Current.Items.Remove(objectContextKey); } } base.Dispose(); }
不能使用单例模式,静态方法就可以。
原文地址:https://www.cnblogs.com/hofmann/p/11750586.html
时间: 2024-10-22 21:09:03