title: EF Core, A second operation started on this context, Context Inhertiance reuse connections
body: <p>I'm trying understand how to create a context with two lifetimes, I'm running async code and I need to separate my contexts so they don't run under the same connection, EntityFramework However, seems to be reusing connections on separate contexts, which is causing this error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'A second operation started on this context before a previous operation completed. </p>
  </blockquote>
  
  <p>In my <code>StartUp</code> I register two contexts:</p>
  
  <pre><code>string connection = @"Server=localhost;Database=Custom;Trusted_Connection=True;";
  
  services.AddEntityFrameworkSqlServer().AddDbContext&lt;CustomContext&gt;(
                  options =&gt; options.UseSqlServer(connection))
          // I've also tried without this, it's weird but it still maps
          .AddDbContext&lt;IsolatedCustomContext&gt;(
                  options =&gt; options.UseSqlServer(connection));
  
  services.AddTransient&lt;DbContext, CustomContext&gt;();
  services.AddTransient&lt;IsolatedCustomContext&gt;();
  </code></pre>
  
  <p>My isolated context is a the same as the custom context so I've declared like so:</p>
  
  <pre><code>public class IsolatedCustomContext : CustomContext
  {
      public IsolatedCustomContext(DbContextOptions&lt;CustomContext&gt; options,
          DbAuthorizationOptions&lt;CustomContext&gt; authorizationOptions, 
          IMemoryCache memoryCache) : base(options, authorizationOptions, memoryCache)
      {
      }
  }
  </code></pre>
  
  <p>and my custom context is declared like so:</p>
  
  <pre><code>public partial class CustomContext: DbContext
  {
      public CustomContext(DbContextOptions&lt;CustomContext&gt; options,
           DbAuthorizationOptions&lt;CustomContext&gt; authorizationOptions, IMemoryCache memoryCache) : base(options, authorizationOptions, memoryCache)
      {
      }
  
      public virtual DbSet&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p>In my <code>Startup</code> I sync the user profile with my identity server like so:</p>
  
  <pre><code>o.JwtBearerEvents = new JwtBearerEvents
  {
      OnTokenValidated = async tvc =&gt; { await AuthenticationRule.ValidateToken(tvc); }
  };
  </code></pre>
  
  <p>My authentication uses the isolated context to update user info:</p>
  
  <pre><code>// Using shouldn't be required but I've tried with it and without it
  using (var context = serviceProvider.GetRequiredService&lt;IsolatedAstootContext&gt;())
  {
      var userId = await context.Users
                                .Where(u =&gt; u.UniqueIdentifier == subjectId)
                                .Select(x =&gt; x.Id)
                                .FirstOrDefaultAsync();
  }
  </code></pre>
  
  <p>Looks like the Pipeline Split's and runs concurrently after the token is validate to execute the request  I have a controller that looks like so:</p>
  
  <pre><code>[Route("api/[controller]")]
  public UsersController : Controller
  {
       CustomContext _context;
  
       public UsersController(CustomContext context)
       {
            this._context = context;
       }
  
       [HttpGet]
       public User GetCurrentUser()
       {
           var userId = User.GetUserIdFromClaims();
           return this._context.Users.Where(x =&gt; x.Id == userId).FirstOrDefaultAsync();
       }
  }
  </code></pre>
  
  <p>When I call <code>GetCurrentUser</code>, I get the concurrency error,  I can see in the call stack it has not exited the isolated context using still. What's weird is they are two separate contexts.</p>
  
  <p>is it possible that by inheriting context, EntityFrameWork is Viewing this context as the same and some how reusing the connection?</p>
  
  <p>Does anyone know how I can register these so they can operate parallel over different connections?</p>