码迷,mamicode.com
首页 > Windows程序 > 详细

C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

时间:2019-10-28 10:29:26      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:ati   been   span   text   实例   shc   cti   rri   err   

 

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

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

 

C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

标签:ati   been   span   text   实例   shc   cti   rri   err   

原文地址:https://www.cnblogs.com/hofmann/p/11750586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!