---
title: "ObjectDisposedException While using Include"  
description: "ObjectDisposedException While using Include"  
author: "Anonymous User"  
published: 2014-08-29  
updated: 2014-09-01  
canonical: https://www.mindstick.com/forum/2203/objectdisposedexception-while-using-include  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# ObjectDisposedException While using Include

My page calls a [Services](https://www.mindstick.com/articles/13111/distinctions-of-iiot-services-how-its-helps-brands) layer method that uses a Generic [Repository](https://www.mindstick.com/articles/12232/repository-pattern-in-mvc) "Find" method. In the services layer method, I do the following:

```
 using (IUnitOfWork unitOfWork = new DBContext())                {                   
GenericRepository<Operator> operatorRepos = new GenericRepository<Operator>(unitOfWork);     {                       
try                       
{                           
var oper = operatorRepos.Find(o => o.OperatorID ==operatorID).Include(o => o.cmn_Address).Single();                           
return oper;                       
}                       
catch (InvalidOperationException exc)                       
{                           
//handle exception                       
}                    }                }
```

The Find method for my repository:

```
public IQueryable<T> Find(Func<T, bool> predicate)        {            return _objectSet.Where<T>(predicate).AsQueryable();        }
```

On the page, I try to [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the cmn_address [Navigation](https://www.mindstick.com/blog/62/different-navigation-in-asp-dot-net) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) of the [Operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) and I get the following error:

The [ObjectContext](https://www.mindstick.com/interview/34244/explain-the-difference-between-dbcontext-and-objectcontext) [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) has been disposed and can no longer be used for [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) that require a connection.

I realize that this is caused by the using statement to dispose of the [context](https://www.mindstick.com/forum/23245/what-is-context-in-android), but I thought the Include method will eager load the cmn_Address object. I don't understand why this doesn't work as I expected.

## Replies

### Reply by Sumit Kesarwani

Hi Ashish,

You are using Func<> instead of Expression<Func<>> in your where condition. That makes it Linq-to-objects. This change is permanent. Calling AsQueryable doesn't make it Linq-to-entities again.


---

Original Source: https://www.mindstick.com/forum/2203/objectdisposedexception-while-using-include

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
