Skip to content

fix: A second operation was started in DataGrid with EF Core #801

@davidkudera

Description

@davidkudera

🐛 Bug Report

DataGrid with Paginator in version 3.1.0 and 3.1.1 fails with the following error:

A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.

Sometimes it loads the first time after starting the app but fails on the next request.

💻 Repro or Code Sample

builder.Services.AddFluentUIComponents(options =>
{
    options.HostingModel = BlazorHostingModel.Server;
});

builder.Services.AddDataGridEntityFrameworkAdapter();
<FluentDataGrid Items="@_files" Pagination="_pagination" TGridItem="@File">
    <PropertyColumn Title="Name" Property="@((File f) => f.Name)" Sortable="true" IsDefaultSortColumn="true" InitialSortDirection="SortDirection.Ascending" />
</FluentDataGrid>

<FluentPaginator State="@_pagination" />

@code {

    private readonly PaginationState _pagination = new() { ItemsPerPage = 20 };

    private ApplicationContext? _context;

    private DbSet<File>? _files;

    public void Dispose()
    {
        _context?.Dispose();
    }

    protected override async Task OnInitializedAsync()
    {
        _context = await ContextFactory.CreateDbContextAsync();
        _files = _context.Files;
    }

}

🤔 Expected Behavior

Not to fail.

😯 Current Behavior

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: 28.09.2023 11:19:01.627 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'ApplicationContext'.
      System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
         at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
      System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
         at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
fail: 28.09.2023 11:19:01.630 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query)
      An exception occurred while iterating over the results of a query for context type 'ApplicationContext'.
      System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
         at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
      Unhandled exception rendering component: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
      System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
         at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
         at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
         at lambda_method1928(Closure, QueryContext)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
         at Microsoft.Fast.Components.FluentUI.FluentDataGrid`1.<BuildRenderTree>b__113_1(RenderTreeBuilder __builder3) in /_/src/Core/Components/DataGrid/FluentDataGrid.razor:line 32
         at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)
         at Microsoft.Fast.Components.FluentUI.DataGrid.Infrastructure.Defer.BuildRenderTree(RenderTreeBuilder builder) in /_/src/Core/Components/DataGrid/Infrastructure/Defer.cs:line 26
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit '1LCf-dBwf8NBgLlwQAp6gSqC2UtUGsOCS6o62lEQi3c'.
      System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
         at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
         at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
         at lambda_method1928(Closure, QueryContext)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
         at Microsoft.Fast.Components.FluentUI.FluentDataGrid`1.<BuildRenderTree>b__113_1(RenderTreeBuilder __builder3) in /_/src/Core/Components/DataGrid/FluentDataGrid.razor:line 32
         at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)
         at Microsoft.Fast.Components.FluentUI.DataGrid.Infrastructure.Defer.BuildRenderTree(RenderTreeBuilder builder) in /_/src/Core/Components/DataGrid/Infrastructure/Defer.cs:line 26
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: 28.09.2023 11:19:01.639 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (2ms) [Parameters=[@__p_1='20', @__p_0='0'], CommandType='Text', CommandTimeout='30']
      SELECT f.id, f.container_name, f.created_at, f.mime, f.name, f.resolution_v2, f.size
      FROM public.file AS f
      ORDER BY f.name
      LIMIT @__p_1 OFFSET @__p_0
info: 28.09.2023 11:19:01.647 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (2ms) [Parameters=[@__p_1='20', @__p_0='0'], CommandType='Text', CommandTimeout='30']
      SELECT f.id, f.container_name, f.created_at, f.mime, f.name, f.resolution_v2, f.size
      FROM public.file AS f
      ORDER BY f.name
      LIMIT @__p_1 OFFSET @__p_0
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: 28.09.2023 11:19:01.674 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f
info: 28.09.2023 11:19:01.706 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT count(*)::int
      FROM public.file AS f

💁 Possible Solution

Downgrade to version 3.0.1

🔦 Context

Print a simple DataGrid with data from DbSet.

🌍 Your Environment

  • OS & Device: Windows 11
  • Browser: Brave, Chromium 117
  • .NET: 7.0.201
  • Blazor mode: server
  • Microsoft.Fast.Components.FluentUI: 3.1.1
  • Microsoft.Fast.Components.FluentUI.DataGrid.EntityFrameworkAdapter: 3.0.0
  • EF Core with PostgreSQL: 7.0.10
  • Npgsql: 7.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions