title: How is my DbContext being disposed?
body: <p>I am having a great deal of difficulty in splitting my monolithic ASP.NET MVC application into an N-tier app. In the following example, during the first call to <strong>_messageRepo.Create()</strong> an exception is thrown saying that the <strong>DbContext</strong> cannot be used since it was already disposed. </p>
  
  <p>I cannot see how that is happening, and attempting to break on the <strong>Dispose()</strong> method doesn't actually cause the application to break during debugging.</p>
  
  <p>The basic structure is as follows:</p>
  
  <ul>
  <li>Controllers are injected with instances of each service they use //ie: public MyController(IMessageService messageService)</li>
  <li>Services contain any needed instances of repositories (ie: <strong>_messageRepository</strong>)</li>
  <li>Repositories utilize an instance of <strong>MyContext</strong>, a sub-class of <strong>DbContext</strong></li>
  <li><p>These instances are reconstructed whenever needed as in the following example </p>
  
  <pre><code>using(var context = new MyContext())
  {
      _messageRepo = new MessageRepository(context);
      _idRepo = new IdentityRepository(context);
  
      var status = _messageRepo.GetStatus(Int32.Parse(message.To));
      message.To = status.Header.From.Name;
      message.ToHash = Obfuscate.SaltAndHash(message.To);
      message.Subject = "RE:" + status.Header.Subject;
  
      var toUser = _idRepo.Get(message.To);
      var fromUser = _idRepo.Get(_userName);
      var rawMessage = new Message()
      {
          Content = message.Content,
          Attachments = GetAttachments(message.AttachmentIds)
      };
      var header = new MessageHeader()
      {
          To = toUser,
          From = fromUser,
          Subject = message.Subject
      };
      _messageRepo.Create(new MessageStatus()
      {
          CreatedAt = DateTime.Now,
          IsRead = false,
          IsSpam = false,
          IsTrash = false,
          Message = rawMessage,
          Header = header,
          Owner = header.To
      });
      _messageRepo.Create(new MessageStatus()
      {
          CreatedAt = DateTime.Now,
          IsRead = false,
          IsSpam = false,
          IsTrash = false,
          Message = rawMessage,
          Header = header,
          Owner = header.From
      });
      context.Commit();
      Email.SendNewMessageNotification(fromUser.Email, toUser.Email);
  }
  </code></pre></li>
  </ul>
  
  <p>The repository methods are LINQ one-liners that retrieve models from a database through Entity Framework using the code first approach.</p>
  
  <p>Is there something wrong with this approach? I did have MyContext implement IUnitOfWork at first, but I removed that until I get this less complicated method functional.</p>
  
  <p>Additionally, I am using an IoC framwork (AutoFac) to load instances of these interface implementations. If this is the problem, then what do I need to change about my logic to accomodate AutoFac?</p>
  
  <pre><code>//in Global.asax.cs
  builder.RegisterType&lt;PonosContext&gt;().As&lt;PonosContext&gt;().InstancePerHttpRequest();
  
  //Example repo constructor
  public MessageRepository(PonosContext context)
  {
      _db = context;
  }
  </code></pre>