title: ASP.Net Core MVC Repository Pattern Unexpectedly disposing
body: <p>When I try to add a comment, I get the following error:</p>
  
  <blockquote>
    <p>ObjectDisposedException: Cannot access a disposed object.</p>
  </blockquote>
  
  <p>When the code runs the second line:</p>
  
  <pre><code>m_context.Comments.Add(comment);
  m_context.SaveChanges();
  </code></pre>
  
  <p>Why is the context being disposed? If move the the TryAddComment method into the controller, it doesn't call Dispose early.</p>
  
  <p>Here is what my Controller and Repository class look like (simplified).</p>
  
  <p>CommentsController.cs:</p>
  
  <pre><code>public class CommentsController : Controller
  {
  
      private ICommentRepository m_commentRepository;
  
      public CommentsController(ICommentRepository commentRepository)
      {
          m_commentRepository = commentRepository;
      }
  
      // POST: api/Comments
      [HttpPost]
      public async Task&lt;IActionResult&gt; PostComment([FromBody] CommentAddViewModel commentVM)
      {
          Comment comment = new Comment
          {
              ApplicationUserId = User.GetUserId(),
              PostId = commentVM.PostId,
              Text = commentVM.Text
          };
  
          bool didAdd = m_commentRepository.TryAddComment(comment);
  
          if (!didAdd)
          {
              return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
          }
  
          return CreatedAtRoute("GetComment", new { id = comment.CommentId }, comment);
      }
  
  }
  </code></pre>
  
  <p>CommentRepository.cs:</p>
  
  <pre><code>public class CommentRepository : ICommentRepository, IDisposable
  {
  
      public ApplicationDbContext m_context;
  
      public CommentRepository(ApplicationDbContext context)
      {
          m_context = context;
      }
      public bool TryAddComment(Comment comment)
      {
          m_context.Comments.Add(comment);
          m_context.SaveChanges();
  
          return true;
      }
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  m_context.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>If I use a local CommentRepository, it works as expected. For example:</p>
  
  <pre><code>    CommentRepository localCommentRepo = new CommentRepository(m_context);
      bool didAdd = localCommentRepo.TryAddComment(comment);
  </code></pre>
  
  <p>Edit2:</p>
  
  <p>In Startup.cs, I registered the IcommentRepository as Scoped and works as expected. Originally it was Singleton. Why would a singleton cause this issue?</p>
  
  <pre><code>services.AddSingleton&lt;ICommentRepository, CommentRepository&gt;(); //breaks
  services.AddScoped&lt;ICommentRepository, CommentRepository&gt;(); //works
  </code></pre>
  
  <p>Edit3:</p>
  
  <p>ApplicationDbContext.cs:</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      protected override void OnModelCreating(ModelBuilder builder)
      {
          base.OnModelCreating(builder);
          // Customize the ASP.NET Identity model and override the defaults if needed.
          // For example, you can rename the ASP.NET Identity table names and more.
          // Add your customizations after calling base.OnModelCreating(builder);
  
      }
      public DbSet&lt;Post&gt; Posts { get; set; }
      public DbSet&lt;Comment&gt; Comments { get; set; }
  }
  </code></pre>