该ObjectContext的实例已处置 - 的WinForms实体框架 [英] The ObjectContext instance has been disposed - Winforms Entity Framework

查看:120
本文介绍了该ObjectContext的实例已处置 - 的WinForms实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决这个问题,也阅读了有关这个错误的内容,但无法找出一个解决方案。
我建立使用实体框架,一个简单的产品分类情况WinForms应用程序。
这里是我的模型的快照。



在ProductService的代码类,检索所有的产品是

 公共静态列表<产品与GT; GetAllProducts()
{
名单,LT;产品与GT;产品=新的List<产品及GT;();使用
(VAR实体=新SUIMSEntities1())
{
产品=(从entity.Products
p选择P).ToList();
回报的产品;
}
}

代码在产品后面的代码是



 列表<产品与GT;触头= ProductServices.GetAllProducts(); 
dgvProducts.DataSource =电棒;

当我尝试加载在datagridview的产品,显示了以下错误:
< IMG SRC =htt​​p://i.stack.imgur.com/6Lxyb.pngALT =在这里输入的形象描述>



你能告诉我是什么?原因造成的问题。



编辑:
中包括的伎俩,在这种特定的情况下,我改变了GetAllProducts()作为以下

 公共静态列表<产品与GT; GetAllProducts()
{
使用(VAR实体=新SUIMSEntities1())
{
名单,LT;产品与GT;产品= entity.Products.Include(类别)了ToList()。
回报的产品;
}
}


解决方案

通过默认情况下,实体框架(EF)将懒加载类对象集。由于设置类别的对象是懒加载,当其他一些代码稍后引用类别,将EF尝试加载集。然而,在这一点上,你的背景已经被处置,致使您所看到的错误。



您需要做的是强制的背景下有什么急切地加载类的实体设置像这样:

 公共静态列表<产品与GT; GetAllProducts()
{
名单,LT;产品与GT;产品=新的List<产品及GT;();使用
(VAR实体=新SUIMSEntities1())
{
entity.Include(类别); //类别
产品力预先加载=(从entity.Products
p选择P).ToList();
回报的产品;
}
}


I am trying to solve this problem and did read content regarding this error but was unable to figure out a solution. I am building a winforms application using Entity framework for a simple Products Categories scenario. Here is a snapshot of my model.

The code in ProductService class that retrieves all the products is

public static List<Product> GetAllProducts()
{
    List<Product> products = new List<Product>();
    using (var entity = new SUIMSEntities1())
    {
        products = (from p in entity.Products
                    select p).ToList();
        return products;
    }            
}

Code in the Products code behind is

List<Product> prods=ProductServices.GetAllProducts();
dgvProducts.DataSource = prods;

When I try to load the Products in datagridview, the following error is shown:

Could you please tell me what is causing the problem?

Edit: The Include did the trick and in this specific scenario I changed the GetAllProducts() as below

        public static List<Product> GetAllProducts()
        {
            using (var entity = new SUIMSEntities1())
            {
                List<Product> products = entity.Products.Include("Category").ToList();
                return products;                
            }            
        }

解决方案

By default, entity framework (EF) will lazy load your Category object set. Because your Category object set is lazy loaded, when some other code later on references a Category, EF will try to load the set. However, at this point, your context has been disposed, resulting in the error you are seeing.

What you need to do is force the context to eagerly load your Category entity set like so:

public static List<Product> GetAllProducts()
{
    List<Product> products = new List<Product>();
    using (var entity = new SUIMSEntities1())
    {
        entity.Include("Category");  //force eager loading of Category
        products = (from p in entity.Products
                    select p).ToList();
        return products;
    }            
}

这篇关于该ObjectContext的实例已处置 - 的WinForms实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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