forum

Home / DeveloperSection / Forums / ObjectDisposedException While using Include

ObjectDisposedException While using Include

Anonymous User 1722 29-Aug-2014

My page calls a Services layer method that uses a Generic Repository "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 the cmn_address Navigation property of the Operator and I get the following error:

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

I realize that this is caused by the using statement to dispose of the context, 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.


c# c# 
Updated on 01-Sep-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By