[Blazor] Improve the experience with QuickGrid and EF Core (original) (raw)

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.

--- a/QuickGridTestProj/Components/Pages/ResourceOverview.razor +++ b/QuickGridTestProj/Components/Pages/ResourceOverview.razor @@ -27,7 +27,7 @@

 <h3>Resources overview</h3>
  • <QuickGrid Class="mt-2" Items="_dbContext.Resources.OrderBy(x => x.ResourceId)" ItemKey="(x => x.ResourceId)" Pagination="Pagination">

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();

Originally posted by @javiercn in #58669