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

萝らか妹 提交于 2019-12-24 04:34:15

问题


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

回答1:


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



回答2:


It sounds like whatever class you have which "owns" the context has disposed of it, and a subsequent call in the line IEnumerable<Orders> ordersList = context.Employees.Include is throwing an error.



来源:https://stackoverflow.com/questions/7354954/entity-framework-error-the-objectcontext-instance-has-been-disposed-and-can-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!