dotnet / dotnet/aspnetcore

[Blazor] Improve the experience with QuickGrid and EF Core

Open
#58,716 4 comments 9 reactions 1 assignee Assigned to @lewing View on GitHub
area-blazor enhancement feature-blazor-quickgrid on-roadmap Priority:1 triaged
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

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 `Items` is triggering multiple concurrent queries, as every time we render it triggers a new query.
>
> The following change will make it work as expected.
>
> ```diff
> --- a/QuickGridTestProj/Components/Pages/ResourceOverview.razor
> +++ b/QuickGridTestProj/Components/Pages/ResourceOverview.razor
> @@ -27,7 +27,7 @@
>


>
>

Resources overview


> -
> +
>
>
>
> 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> GetItems(
> + GridItemsProviderRequest request)
> + {
>
> + using var context = _dbContextFactory.CreateDbContext();
> + var totalCount = await context.Resources.CountAsync(request.CancellationToken);
> + IQueryable 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
> + {
> + Items = items,
> + TotalItemCount = totalCount
> + };
>
> + return result;
> + }
> +
> private async Task OnAddResourceSubmit()
> {
> using var dbContext = _dbContextFactory.CreateDbContext();
> ```

_Originally posted by @javiercn in [#58669](https://github.com/dotnet/aspnetcore/issues/58669#issuecomment-2447796760)_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.