ObjectContext中实例已设置,并且可以不再使用 [英] The ObjectContext instance has been disposed and can no longer be used

查看:115
本文介绍了ObjectContext中实例已设置,并且可以不再使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题问了万次,但我遇到的每一个建议,我似乎已经涵盖了。

I've seen this question asked a million times, but every single suggestion I've encountered I already seem to have covered.

我在我的MVC解决方案的实体框架,我有一个'仓库',它试图检索为MyObject 的系列S:

I have entity framework in my MVC solution, and I have a 'repository' which attempts to retrieve a collection of MyObjects:

public static List<MyObject> GetMyObjects()
{
   using (var context = new MyEntities())
   {
       return context.MyObjects.Include("Contact").OrderByDescending(o => o.Date).ToList();
   }
}

我把这种方法,它试图序列化控制器行动的一部分:

I call this method as part of a controller action which attempts to serialize it:

public JsonResult All()
{
   return Json(MyRepository.GetMyObjects(), JsonRequestBehavior.AllowGet);
}

和我得到以下错误:

的ObjectContext的实例已经被处置,不能再用于需要连接操作。

我不知道要找谁就这一个,我AP preciate实体框架懒加载关系型的数据集,如果当他们需要,我AP preciate试图序列化整个系列的确会试图加载这些(using语句之外),但我有包含在那里。

I don't know where to turn on this one, I appreciate entity framework 'lazy-loads' relational data sets if and when they're needed, and I appreciate attempting to serialize the whole collection would indeed attempt to load these (outside of the using statement), but I have the 'Include' there.

我已经试过

我已经试过了'包括',我也没有保证其他协会是为MyObject 类的一部分(即我没有其他的包括() s到写)。

I've tried the 'include', I've also ensured no other associations are part of the MyObject class (i.e. I have no other Include()s to write).

推荐答案

要避免这一点,你有一些options.Don't声明你的导航属性为虚拟或禁用< STRONG>延迟加载在您的环境行为。延迟加载默认情况下启用并通过创建派生代理类型的实例,然后覆盖虚拟属性以添加装载钩来实现的。所以,如果你想用一个串行的工作,我建议您关闭延迟加载:

To avoid this you have some options.Don't declare your navigation properties as virtual or disable Lazy Loading behavior on your context. Lazy loading is enable by default and is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook. So, if you want to work with a serializer I recommend you turn off lazy loading:

public class YourContext : DbContext 
{ 
    public YourContext() 
    { 
        this.Configuration.LazyLoadingEnabled = false; 
    } 
}

这些链接可以帮助您更好地了解我在我的答案解释:

These links can help you to understand better what I explain in my answer:

如果您从您的导航属性中删除虚拟关键字,该POCO实体不符合在第二环节中的要求,因此,EF不会创建一个代理类懒加载您的导航性能。但是,如果你禁用延迟加载,即使您的导航性能是虚拟,他们将不会在任何实体加载。这是很好的主意禁用延迟加载,当您使用的是串行器。最串行通过在类型的实例访问每个属性工作

If you remove the virtual keyword from your navigation properties, the POCO entity not meet the requirements described in the second link, so, EF won't create a proxy class to lazy load your navigation properties. But if you disabled lazy loading, even when your navigation properties are virtual, they won't be loaded in any entity. It's good idea disable lazy loading when you are using a serializer. Most serializers work by accessing each property on an instance of a type.

作为第三个选择,你可以使用 JsonIgnore 上的属性你不想要导航属性被序列化作为实体的一部分,但正如我前面所说的,最好的办法是禁用延迟加载。

As a third option you could use the JsonIgnore attribute on your navigation properties that you don't want to be serialized as part of your entity, but as I said before, the best option is disable lazy loading.

这篇关于ObjectContext中实例已设置,并且可以不再使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆