title: EF Core MySql connections not closing
body: <p>We have developed a project in .NET Core and Entity Framework Core using the MySql nuget package. </p>
  
  <p>The context is added to dependancy injection using the following line:</p>
  
  <pre><code>services.AddDbContext&lt;ReadWriteContext&gt;(options =&gt; options.UseMySQL(Configuration["Machine:ReadWriteConnectionString"]));
  </code></pre>
  
  <p>Then in a controller, this is injected as such:</p>
  
  <pre><code>public class SystemController : Controller
  {
      private readonly ReadWriteContext _dataContext;
  
      public SystemController(ReadWriteContext dataContext)
      {
          _dataContext = dataContext;
      }
  
      ...
  
  }
  </code></pre>
  
  <p>And used as such:</p>
  
  <pre><code>var hasServices = await _dataContext.Services.AnyAsync();
  </code></pre>
  
  <p>In the logs we see the opening and closing log lines:</p>
  
  <pre><code>Opening connection to database 'config_service' on server '10.211.55.5'.
  Closing connection to database 'config_service' on server '10.211.55.5'.
  </code></pre>
  
  <p>However, when we look at the MySql server and run "show full processlist", the connections are still showing as being in the sleep state and never close. When you stop the .NET process, the connections then close and disappear from MySql process list.</p>
  
  <p>How do I get the connections to close when the request is finished. The AddDbContext should be scoped to the current request, but it does not appear to properly close the connections.</p>
  
  <p>Any help would be great?</p>