title: DbContext has been disposed
body: <p>I developed a web application with ASP.NET MVC 4 and SQL Server 2008, I create ContextManager class to have only one database context in all pages.</p>
  
  <pre><code>public static class ContextManager
  {
      public static HotelContext Current
      {
          get
          {
              var key = "Hotel_" + HttpContext.Current.GetHashCode().ToString("x")
                        + Thread.CurrentContext.ContextID.ToString();
              var context = HttpContext.Current.Items[key] as HotelContext;
              if (context == null)
              {
                  context = new HotelContext();
                  HttpContext.Current.Items[key] = context;
              }
              return context;
          }
      }
  }
  </code></pre>
  
  <p>It works properly in most of the pages, but in registration page something goes wrong and my context gone deposed with following error: </p>
  
  <blockquote>
    <p>The operation cannot be completed because the DbContext has been disposed.</p>
  </blockquote>
  
  <pre><code>public ActionResult Register ( RegisterModel model )
  {
      if ( ModelState.IsValid )
      {
          // Attempt to register the user
          try
          {
              WebSecurity.CreateUserAndAccount( model.UserName, model.Password,
                                                new
                                                 {
                                                        Email = model.Email,
                                                        IsActive = true,
                                                        Contact_Id = Contact.Unknown.Id
                                                 } );
  
              //Add Contact for this User.
              var contact = new Contact { Firstname = model.FirstName, LastName = model.Lastname };
              _db.Contacts.Add( contact );
              var user = _db.Users.First( u =&gt; u.Username == model.UserName );
              user.Contact = contact;
              _db.SaveChanges();
              WebSecurity.Login( model.UserName, model.Password );
  </code></pre>
  
  <p>at the line <code>_db.Contacts.Add( contact );</code> I got the exception.</p>
  
  <p>But without using ContextManager by changing</p>
  
  <pre><code>HotelContext _db = ContextManager.Current;
  </code></pre>
  
  <p>into:</p>
  
  <pre><code>HotelContext _db = new HotelContext();
  </code></pre>
  
  <p>the problem was solved. But I need to use my own ContextManager. What is the problem?</p>