实体框架错误:ObjectContext的实例已被释放,并可以不再被用于需要连接的操作 [英] Entity Framework Error : The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

查看:193
本文介绍了实体框架错误:ObjectContext的实例已被释放,并可以不再被用于需要连接的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架与业务之后,DAL和底座接口。我继承了IDispose接口在我的仓库,我收到以下错误试图让这个名单了。大部分我所遇到的例子中建议使用IEnumerable和添加.ToList()进行查询,我已经看到下面。我怎样才能解决这个?这是在其他地方,我也有类似的多个相关实体的查询,我不工作明白为什么即时得到错误吗?如果有人能指出一个例子在code如何解决这一问题,这将是巨大的。

 公开的IEnumerable<订单> GetOrdersByCustomer(INT CUSTOMER_ID)
{
    IEnumerable的<订单> ordersList = context.Employees
        .INCLUDE(订单)
        .INCLUDE(客户)
        。凡(C => c.Customers.customer_id == CUSTOMER_ID)
        .ToList();

    返回ordersList;
}
 

解决方案

我发现它不是工作的原因,其实我不得不做一些像

 公开的IEnumerable<订单> GetOrdersByCustomer(INT CUSTOMER_ID)
{
    IEnumerable的<订单> ordersList = context.Employees
        .INCLUDE(客户)
        .INCLUDE(Customers.Orders)
        。凡(C => c.Customers.customer_id == CUSTOMER_ID)
        .ToList();
    返回ordersList;
}
 

I am using Entity Framework with a business Later, DAL and a base Interface. I am inheriting the IDispose interface in my repository, I am getting the following error trying to get this list back. most of the examples I have come across suggest using IEnumerable and add .ToList() for the query and I have already as seen below. How can I get around this? This is working in other places where I have similar multiple related entity queries, I dont understand why im getting the error here? If someone can point out with an example in code how to fix this that would be great.

public IEnumerable<Orders> GetOrdersByCustomer(int customer_id)
{
    IEnumerable<Orders> ordersList = context.Employees
        .Include("Orders")
        .Include("Customers")
        .Where(c => c.Customers.customer_id == customer_id)
        .ToList();

    return ordersList;
}

解决方案

I found the reason it wasnt working actually I had to do something like

public IEnumerable<Orders> GetOrdersByCustomer(int customer_id)
{              
    IEnumerable<Orders> ordersList = context.Employees
        .Include("Customers")
        .Include("Customers.Orders")
        .Where(c => c.Customers.customer_id == customer_id)
        .ToList();          
    return ordersList;
} 

这篇关于实体框架错误:ObjectContext的实例已被释放,并可以不再被用于需要连接的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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