title: The operation cannot be completed because the DbContext has been disposed
body: <p>I try to make custom error handling (http://perspectivespace.com/100497697) with error module(Application_Error), HttpModuleMagic.MVC3, Ninject.MVC3, EF 4.1. Unfortunately when I want to log the error into the database it gives this error message:
  "The operation cannot be completed because the DbContext has been disposed."</p>
  
  <p>Could somebody help me, please?</p>
  
  <pre><code>public class ErrorHandlerHttpModule : IHttpModule
  {
      private const string ErrorControllerRouteName = "Error";
      private IErrorRepository errorRepo;
      private IUserRepository userRepo;
  
    //  private IUserRepository UserRepository;
  
      public ErrorHandlerHttpModule(IErrorRepository er, IUserRepository ur)
      {
          errorRepo = er;
          userRepo = ur;
      }
  
      public void Dispose()
      {
      }
  
      public void Init(HttpApplication context)
      {
          context.Error += Application_Error;
      }
  
      private void Application_Error(object sender, EventArgs e)
      {
          // Get the last error.
  
          var exception = HttpContext.Current.Server.GetLastError();
      ...
  
      error.User = userRepo.GetUserByName(name);
  
      ...
  </code></pre>
  
  <p>In the NinjectWebCommon.cs:</p>
  
  <pre><code>    private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind&lt;IErrorRepository&gt;().To&lt;ErrorRepository&gt;().InRequestScope();
          kernel.Bind&lt;IUserRepository&gt;().To&lt;UserRepository&gt;().InRequestScope();
  
          kernel.Bind&lt;IDatabaseFactory&gt;().To&lt;DatabaseFactory&gt;().InRequestScope();
  
          kernel.Bind&lt;IDisposable&gt;().To&lt;Disposable&gt;().InRequestScope();
          kernel.Bind&lt;IHttpModule&gt;().To&lt;ErrorHandlerHttpModule&gt;();
      } 
  </code></pre>
  
  <p>... and the UserRepository class:</p>
  
  <pre><code>public class UserRepository : RepositoryBase&lt;User&gt;, IUserRepository
  {
      public UserRepository(IDatabaseFactory databaseFactory)
      : base(databaseFactory)
      {
      } 
  
      public User GetUserByName(string name)
      {
          User us = null;
          try
          {
              us = dataContext.Users.Where(u =&gt; u.UserName.Equals(name)).FirstOrDefault() as User;
          }
          catch (Exception ex)
          {
  
          }
          return us;
      }
  }
  </code></pre>
  
  <p>The RepositoryBase:</p>
  
  <pre><code>public abstract class RepositoryBase&lt;T&gt; where T : class
  {
      protected TestContext dataContext;
      private readonly IDbSet&lt;T&gt; dbset;
      protected RepositoryBase(IDatabaseFactory databaseFactory)
      {
          DatabaseFactory = databaseFactory;
          dbset = DataContext.Set&lt;T&gt;();
      }
  
      protected IDatabaseFactory DatabaseFactory
      {
          get;
          private set;
      }
  
      protected TestContext DataContext
      {
          get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
      }
      ...
  </code></pre>
  
  <p>and DatabaseFactory class:</p>
  
  <pre><code>public class DatabaseFactory : Disposable, IDatabaseFactory
  {
      private TestContext dataContext;
  
      public TestContext Get()
      {
          return dataContext ?? (dataContext = new TestContext());
      }
  
      protected override void DisposeCore()
      {
          if (dataContext != null)
              dataContext.Dispose();
      }
  }
  </code></pre>
  
  <p>This line dives the error:</p>
  
  <pre><code>dataContext.Users.Where(u =&gt; u.UserName.Equals(name)).FirstOrDefault() as User;
  </code></pre>
  
  <p>I try to follow this article:
  <a href="http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx" rel="nofollow">http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx</a></p>
  
  <p>Thank you for your answer.</p>