Closed as not planned
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I'm using QuickGrid in a Blazor .Net 8 WebApp with Server interactive mode.
I have a page displaying a list of resources and a button in each row to delete the resource. Sometimes, when I click on the delete button, I get an error:
An exception occurred while iterating over the results of a query for context type 'DbContext'.
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.
The code is quite basic
@page "/overview"
@using Microsoft.AspNetCore.Components.QuickGrid
@using TestProject.Data.Contexts
@rendermode @(new InteractiveServerRenderMode(prerender: false))
@implements IAsyncDisposable;
<div class="pt-4">
<QuickGrid Class="mt-2" Items="_dbContext.Resources.OrderBy(x => x.ResourceId)" ItemKey="(x => x.ResourceId)" Pagination="Pagination">
<PropertyColumn Property="@(p => p.ResourceName)" Title="Name" Sortable="true" />
<PropertyColumn Property="@(p => p.ResourceDescription)" Sortable="true" />
<TemplateColumn>
<button class="btn btn-outline-secondary btn-sm ms-1" @onclick="(() => Delete(context.ResourceId))">Delete</button>
</TemplateColumn>
</QuickGrid>
<Paginator State="Pagination" />
</div>
And the code in the razor.cs file
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.QuickGrid;
using Microsoft.EntityFrameworkCore;
using TestProject.Data.Contexts;
using TestProject.Data.Entities;
namespace TestProject.Components.Pages
{
public partial class Overview
{
[Inject]
private IDbContextFactory<DbContext> _dbContextFactory { get; set; } = default!;
private MonitoringDbContext _dbContext = default!;
public PaginationState Pagination = new PaginationState { ItemsPerPage = 10 };
protected override void OnInitialized()
{
_dbContext = _dbContextFactory.CreateDbContext();
}
private async Task Delete(int resourceId)
{
using var dbContext = _dbContextFactory.CreateDbContext();
var entity = dbContext .Resources.Find(resourceId);
if (entity != null)
{
dbContext .Resources.Remove(entity);
await dbContext .SaveChangesAsync();
}
}
public async ValueTask DisposeAsync()
{
await _dbContext.DisposeAsync();
}
}
}
I'm using db context factory and creating a new db context for each delete action, and a db context to be used by the quickgrid and it will be disposed when the component is unloaded.
The error is not consistent. Sometimes I'm able to delete multiple items without problems. Sometimes I get the error every time I delete an item.
Is this a bug or am I doing something wrong?
Expected Behavior
No response
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response