How to use injected DbContext in parallel methods in asp.net core and ef7?
<p>I have an Asp.net 5/Core 1 application with EF7. I register the DbContext in the DI container normally.</p>

<pre><code>services.AddEntityFramework().AddSqlServer()
  .AddDbContext&lt;MyDbContext&gt;(options =&gt; options.UseSqlServer(connection));
</code></pre>

<p>Here is a small sample showing what I'm trying to do.</p>

<pre><code>public class MyController : Controller
{
    MyDbContext _myDbContext;

    public MyController(MyDbContext myDbContext)
    {
        _myDbContext = myDbContext;
    }

    public IActionResult Index()
    {
        //Just start these and don't wait for them to finish
        //We don't care if they succeed or not
        Task.Run(() =&gt; DoSomeLogging1());
        Task.Run(() =&gt; DoSomeLogging2());

        return View();
    }

    private void DoSomeLogging1()
    {
        _myDbContext.Items.ToList();
    }

    private void DoSomeLogging2()
    {
        _myDbContext.Items.ToList();
    }
}
</code></pre>

<p>Both DoSomeLoggingX methods will use the MyDbContext instance that was injected to the controller. Since the methods are run at the same time doing db queries at the same time, the other one will invariably fail with</p>

<blockquote>
  <p>The connection was not closed. The connection's current state is
  connecting.</p>
</blockquote>

<p>MyDbContext also uses DI to get some references so I can't use it directly even if I wanted.</p>

<p>How can I run code in parallel and still be able to use my database through entity framework?</p>