DbContext object has been disposed: how can I prevent the object to be disposed?
<p>I'm using asp.net mvc, Entityframework 6 and Unity for DI scenarios.</p>

<p>I couldn't find out why my DbContext object are being disposed too early.</p>

<p>I injected the GenericWoU class into <code>PeopleController</code> class. When I call the ListPerson action everything works ok however the DbContext is disposed. So if I try to edit any person of the list, the following error appears:</p>

<blockquote>
  <p>The operation cannot be completed because the DbContext has been
  disposed</p>
</blockquote>

<p>how can I prevent the DbContext to be disposed so early? </p>

<p>Here is my Generic Unit of Work class:</p>

<pre><code>    public class GenericUoW : IDisposable, IGenericUoW
    {
        private readonly DbContext entities = null;
        public Dictionary&lt;Type, object&gt; repositories = new Dictionary&lt;Type, object&gt;();

        public GenericUoW(DbContext entities)
        {
            this.entities = entities;
        }

    public IRepository&lt;T&gt; Repository&lt;T&gt;() where T : class
    {
        if (repositories.Keys.Contains(typeof(T)) == true)
        {
            return repositories[typeof(T)] as IRepository&lt;T&gt;;
        }

        IRepository&lt;T&gt; repo = new GenericRepository&lt;T&gt;(entities);
        repositories.Add(typeof(T), repo);
        return repo;
    }

    public void SaveChanges()
    {
        entities.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                entities.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
</code></pre>

<p>Here is my <code>GenericRepository</code> class:</p>

<pre><code>class GenericRepository&lt;T&gt; : IRepository&lt;T&gt; where T : class
{
    private readonly DbContext entities = null;
    private DbSet&lt;T&gt; _objectSet;

    public GenericRepository(DbContext _entities)
    {
        entities = _entities;
        _objectSet = entities.Set&lt;T&gt;();
    }

    public IEnumerable&lt;T&gt; GetAll(Func&lt;T, bool&gt; predicate = null)
    {
        if (predicate != null)
        {
            return _objectSet.Where(predicate);
        }

        return _objectSet.AsEnumerable();
    }

    public T Get(Func&lt;T, bool&gt; predicate)
    {
        return _objectSet.First(predicate);
    }

    public void Add(T entity)
    {
        _objectSet.Add(entity);
    }

    public void Attach(T entity)
    {
        _objectSet.Attach(entity);
    }

    public void Delete(T entity)
    {
        _objectSet.Remove(entity);
    }
}
</code></pre>

<p>Here is my ContainerBootstrapper class:</p>

<pre><code>public class ContainerBootstrapper
{
    public static IUnityContainer Initialise()
    {
        var container = BuildUnityContainer();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        return container;
    }
    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        DbContext entities = new TeijonStuffEntities();
        container.RegisterInstance(entities);

        GenericUoW GUoW = new GenericUoW(entities);
        container.RegisterInstance(GUoW);

        MvcUnityContainer.Container = container;
        return container;
    }
}
</code></pre>