C
C#8mo ago
IcyIme

✅ A second operation was started on this context instance before a previous operation completed.

i have two layoust one for mobile and second for pc and when i use mobile layout it throws me this error
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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at GradeProject.Components.Pages.ProfilePage.GetLeaderboardPositionAsync(Int32 score) in c:\Users\peter\Desktop\Grade\GradeProject\GradeProject\Components\Pages\ProfilePage.razor:line 143
at GradeProject.Components.Pages.ProfilePage.OnInitializedAsync() in c:\Users\peter\Desktop\Grade\GradeProject\GradeProject\Components\Pages\ProfilePage.razor:line 135
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at GradeProject.Components.Pages.ProfilePage.GetLeaderboardPositionAsync(Int32 score) in c:\Users\peter\Desktop\Grade\GradeProject\GradeProject\Components\Pages\ProfilePage.razor:line 143
at GradeProject.Components.Pages.ProfilePage.OnInitializedAsync() in c:\Users\peter\Desktop\Grade\GradeProject\GradeProject\Components\Pages\ProfilePage.razor:line 135
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

csharp @using GradeProject.Components.Account.Shared

<MediaQueryList>
<ViewTransitionRouter AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="@(isMobile ? typeof(Layout.MobileLayout) : typeof(Layout.MainLayout))">
<NotAuthorized>
<RedirectToLogin/>
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="[autofocus]"/>
</Found>
</ViewTransitionRouter>
<MediaQuery Media="(max-width: 600px)" @bind-Matches="isMobile" />
</MediaQueryList>

@code {
private bool isMobile;
}
csharp @using GradeProject.Components.Account.Shared

<MediaQueryList>
<ViewTransitionRouter AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="@(isMobile ? typeof(Layout.MobileLayout) : typeof(Layout.MainLayout))">
<NotAuthorized>
<RedirectToLogin/>
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="[autofocus]"/>
</Found>
</ViewTransitionRouter>
<MediaQuery Media="(max-width: 600px)" @bind-Matches="isMobile" />
</MediaQueryList>

@code {
private bool isMobile;
}
3 Replies
exixt
exixt8mo ago
how does GetLeaderBoardPositionAsync look like
Jimmacle
Jimmacle8mo ago
an instance of a EF Core dbcontext cannot be used concurrently, the error is pretty much telling you what's wrong if you're using EF directly in blazor components you should consider injecting IDbContextFactory<YourDbContext> instead of the dbcontext itself because blazor component lifetimes don't match the lifetime you should have for dbcontexts
IcyIme
IcyImeOP8mo ago
thanks

Did you find this page helpful?