C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for opera

 

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();
        }

不能使用单例模式,静态方法就可以。

 

上一篇:ArangoDb学习笔记————High-level operations(三)


下一篇:Educational Codeforces Round 71 (Rated for Div. 2) B - Square Filling