System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

后端 未结 10 2237
清酒与你
清酒与你 2020-12-01 23:21

I am using EF 4 to retrieve a list of Employees.

public ContentResult AutoCompleteResult(string searchText)
{
    List list = Employee.GetAll         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 23:51

    I found the best way to handle this and keep the using statement you just need to use the include, see sample below:

    using (var ctx = new Context(this.connectionString)) {
      var query = ctx.[maintable]
        .Include(x => x.[theothertable])
        .FirstOrDefaultAsync(u => u.UserName.Equals(userName));
    }
    

提交回复
热议问题