title: Why is there no Dispose method to override in .Net Core?
body: <p>I am trying to dispose of my DbContext in my GenericRep from my Generic Service.</p>
  
  <p>My generic repo is implementing the pattern described here:
  <a href="https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application</a></p>
  
  <p>I have set up the first part in my repo:</p>
  
  <pre><code>public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt;, IDisposable
      where T: BaseEntity
  {
      protected readonly ApplicationDbContext _context;
      protected DbSet&lt;T&gt; _dbSet;
      .
      .
      .
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  _context.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  </code></pre>
  
  <p>Now in the article they have this in the StudentController:</p>
  
  <pre><code>  protected override void Dispose(bool disposing)
    {
       studentRepository.Dispose();
       base.Dispose(disposing);
    }
  </code></pre>
  
  <p>They don't have StudentController implementing IDisposable or anything.
  They just seem to be able to override Dispose as a standard overridable method on Object I guess.</p>
  
  <p>So I need to put this in my Generic Service.</p>
  
  <p>Here is my Generic Service:</p>
  
  <pre><code>public class GenericService&lt;T&gt; : IGenericService&lt;T&gt;
      where T : BaseEntity
  {
      private IGenericRepository&lt;T&gt; _genericRepo;
  
      public GenericService(IGenericRepository&lt;T&gt; genericRepo)
      {
          _genericRepo = genericRepo;
      }
      . 
      .
      .
      public override void Dispose(bool disposing)
      {
          _genericRepo.Dispose();
          base.Dispose(disposing);
      }
  </code></pre>
  
  <p>So I have two red squigglies on my code.</p>
  
  <p>1) No suitable method found to override for GenericService.</p>
  
  <p>Is this a standard overridable object in Full Framework but not .Net Core?</p>
  
  <p>2) Object does not contain a definition for Dispose on base.Dispose call.</p>
  
  <p>Is this just done differently somehow in .Net Core?</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Thanks Nkosi for your answer below (Answer 1).</p>
  
  <p>So now I am calling the serviceRepo dispose from my controller like this:</p>
  
  <pre><code>[Route("api/[controller]")]
  public class CasesController : Controller, IDisposable
  {
      private readonly IGenericService&lt;Case&gt; _caseGenericService;
  
      public CasesController(IGenericService&lt;Case&gt; caseGenericService)
      {
          _caseGenericService = caseGenericService;
      }
      .
      .
      .
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  _caseGenericService.Dispose();
                  base.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  </code></pre>
  
  <p>It kind of works, for a few disposes.
  But then it seems to start hanging.</p>