title: Blazor concurrency problem using Entity Framework Core
body: <p><strong>My goal</strong></p>
  
  <p>I want to create a new IdentityUser and show all the users already created through the same Blazor page. This page has:</p>
  
  <ol>
  <li>a form through you will create an IdentityUser</li>
  <li>a third-party's grid component (DevExpress Blazor DxDataGrid) that shows all users using UserManager.Users property. This component accepts an IQueryable as a data source.</li>
  </ol>
  
  <p><strong>Problem</strong></p>
  
  <p>When I create a new user through the form (1) I will get the following concurrency error:</p>
  
  <blockquote>
    <p>InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread-safe.</p>
  </blockquote>
  
  <p>I think the problem is related to the fact that <strong>CreateAsync(IdentityUser user)</strong> and <strong>UserManager.Users</strong> are referring the same DbContext</p>
  
  <p>The problem isn't related to the third-party's component because I reproduce the same problem replacing it with a simple list.</p>
  
  <p><strong>Step to reproduce the problem</strong></p>
  
  <ol>
  <li>create a new Blazor server-side project with authentication</li>
  <li><p>change Index.razor with the following code:</p>
  
  <pre><code>@page "/"
  
  &lt;h1&gt;Hello, world!&lt;/h1&gt;
  
  number of users: @Users.Count()
  &lt;button @onclick="@(async () =&gt; await Add())"&gt;click me&lt;/button&gt;
  &lt;ul&gt;
  @foreach(var user in Users) 
  {
      &lt;li&gt;@user.UserName&lt;/li&gt;
  }
  &lt;/ul&gt;
  
  @code {
      [Inject] UserManager&lt;IdentityUser&gt; UserManager { get; set; }
  
      IQueryable&lt;IdentityUser&gt; Users;
  
      protected override void OnInitialized()
      {
          Users = UserManager.Users;
      }
  
      public async Task Add()
      {
          await UserManager.CreateAsync(new IdentityUser { UserName = $"test_{Guid.NewGuid().ToString()}" });
      }
  }
  </code></pre></li>
  </ol>
  
  <p><strong>What I noticed</strong></p>
  
  <ul>
  <li>If I change Entity Framework provider from SqlServer to Sqlite then the error will never show.</li>
  </ul>
  
  <p><strong>System info</strong></p>
  
  <ul>
  <li>ASP.NET Core 3.1.0 Blazor Server-side</li>
  <li>Entity Framework Core 3.1.0 based on SqlServer provider</li>
  </ul>
  
  <p><strong>What I have already seen</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/58046008/blazor-a-second-operation-started-on-this-context-before-a-previous-operation-co">Blazor A second operation started on this context before a previous operation completed</a>: the solution proposed doesn't work for me because even if I change my DbContext scope from <em>Scoped</em> to <em>Transient</em> I still using the same instance of UserManager and its contains the same instance of DbContext</li>
  <li>other guys on StackOverflow suggests creating a new instance of DbContext per request. I don't like this solution because it is against Dependency Injection principles. Anyway, I can't apply this solution because DbContext is wrapped inside UserManager</li>
  <li><a href="https://www.pmichaels.net/tag/a-second-operation-started-on-this-context-before-a-previous-operation-completed/" rel="noreferrer">Create a generator of DbContext</a>: this solution is pretty like the previous one.</li>
  <li><a href="https://github.com/dotnet/aspnetcore/issues/10448" rel="noreferrer">Using Entity Framework Core with Blazor</a></li>
  </ul>
  
  <p><strong>Why I want to use IQueryable</strong></p>
  
  <p>I want to pass an IQueryable as a data source for my third-party's component because its can apply pagination and filtering directly to the Query. Furthermore IQueryable is sensitive to CUD
   operations.</p>