Repository navigation
Description
Getting QuickGrid to work correctly with EF is hard and requires using the ItemsProvider interface in order to avoid issues with concurrent queries that might be triggered with any action defined on the form.
Switching to ItemsProvider is not trivial (had to look up the implementation from our QuickGrid implementation) and could be simplified with a few additional methods on the request, to apply Skip and Take automatically or a more general ApplyRequestParameters method that applies all of them.
The implementation with EF should be simpler since this is a main scenario.
@pilarodriguez what's happening here is that the code in
Itemsis triggering multiple concurrent queries, as every time we render it triggers a new query.The following change will make it work as expected.
--- a/QuickGridTestProj/Components/Pages/ResourceOverview.razor +++ b/QuickGridTestProj/Components/Pages/ResourceOverview.razor @@ -27,7 +27,7 @@ <div class="pt-4"> <h3>Resources overview</h3> - <QuickGrid Class="mt-2" Items="_dbContext.Resources.OrderBy(x => x.ResourceId)" ItemKey="(x => x.ResourceId)" Pagination="Pagination"> + <QuickGrid TGridItem="QuickGridTest.Data.Entities.Resource" Class="mt-2" ItemsProvider="GetItems" ItemKey="(x => x.ResourceId)" Pagination="Pagination"> <PropertyColumn Property="@(p => p.ResourceName)" Title="Name" Sortable="true" /> <PropertyColumn Property="@(p => p.ResourceType)" Sortable="true" /> <TemplateColumn> diff --git a/QuickGridTestProj/Components/Pages/ResourceOverview.razor.cs b/QuickGridTestProj/Components/Pages/ResourceOverview.razor.cs index 0b00352..af80195 100644 --- a/QuickGridTestProj/Components/Pages/ResourceOverview.razor.cs +++ b/QuickGridTestProj/Components/Pages/ResourceOverview.razor.cs @@ -23,6 +23,21 @@ namespace QuickGridTestProj.Components.Pages _dbContext = _dbContextFactory.CreateDbContext(); } + public async ValueTask<GridItemsProviderResult<Resource>> GetItems( + GridItemsProviderRequest<Resource> request) + { + using var context = _dbContextFactory.CreateDbContext(); + var totalCount = await context.Resources.CountAsync(request.CancellationToken); + IQueryable<Resource> query = context.Resources.OrderBy(x => x.ResourceId); + query = request.ApplySorting(query).Skip(request.StartIndex); + if (request.Count.HasValue) + { + query = query.Take(request.Count.Value); + } + var items = await query.ToArrayAsync(request.CancellationToken); + var result = new GridItemsProviderResult<Resource> + { + Items = items, + TotalItemCount = totalCount + }; + return result; + } + private async Task OnAddResourceSubmit() { using var dbContext = _dbContextFactory.CreateDbContext();
Activity
[-][Blazor] Improve the experience with quickgrid and EF[/-][+][Blazor] Improve the experience with quickgrid and EF Core[/+][-][Blazor] Improve the experience with quickgrid and EF Core[/-][+][Blazor] Improve the experience with QuickGrid and EF Core[/+]Rombicon commentedon Mar 12, 2025
I'm working with the BlazorWebAppMovies sample, so my observation/recommendation stems from that and @guardrex has suggested I should put it here.
The proposed implementation is better, but it still throws an exception, two in fact. In the sample, if you type fast enough or if server-side updates come in quick succession, following exceptions might be thrown: System.InvalidOperationException and System.Threading.Tasks.TaskCanceledException.
The more frequent exception is thrown by the EF Core itself. As I understand from the EF ExecuteAsync documentation:
The less frequent exception is thrown by the SQL provider. It is Micrsofot.Data.SqlClient.SqlException wrapped in System.InvalidOperationException (0x80131904): The request failed to run because the batch is aborted, this can be caused by abort signal sent from client, or another request is running in the same session, which makes the session busy.
Operation cancelled by user..
I suggest following changes to the BlazorWebAppMovies sample and perhaps in here:
Exceptions that need handling:
danroth27 commentedon Jul 10, 2025
Unfortunately, due to resource constraints, this isn't going to make it for .NET 10. Moving this to consider for .NET 11.
0xced commentedon Oct 17, 2025
This a bug in Microsoft.Data.SqlClient: dotnet/SqlClient#26
Here's a workaround that you can apply globally at the EF Core level instead of having that complicated try/catch/when code sprinkled over all the EF Core queries: https://github.com/0xced/efmssql With the workaround applied, you can just catch
OperationCanceledException.los93sol commentedon Mar 10, 2026
Is this an opportunity to get the PRs I submitted last year merged that adds things like empty row templating and multi column sorting?
Remove asynchronous execution of bound IQueryable data and restore th…