Blazor Grid, with EF and IQueriable data

I'm evaluating blazor grid component. I have huge dataset (big table) which is accessed with EF core. I cannot find and good example how to correctly configure grid component with IQueriable data.


What i've tried:

I have Db context, where I have defined db set for example: 

public DbSet<Partner> Partners { get; set; }

On Page I have injected context, configured grid/with data this way:

<SfGrid TValue="Partner" DataSource="@data"...
@code {
    IQueryable<Partner> data = null;
    protected override async Task OnInitializedAsync()
    {
        data = context.Partners;
    }
 }

On first load of page everything looks fine. Data is loaded and visible in grid. But when I try to go to next data page in grid i get error:System.InvalidOperationException: 

A second operation was started on this context 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.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at Syncfusion.Blazor.Grids.SfGrid`1.OnHybridParametersSet()
   at Syncfusion.Blazor.Grids.SfGrid`1.OnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)



So my question is. How to correctly configure blazor datagrid with EF dbset for with IQueriable (of course I don't want to copy whole table to List<data> before attaching to grid...).


9 Replies

MG Matej Golob September 29, 2021 12:31 PM UTC

It looks that I can manage this well (connecting grid to IQueriable) with custom DataAdaptor as described on https://blazor.syncfusion.com/documentation/datagrid/custom-binding



RN Rahul Narayanasamy Syncfusion Team October 1, 2021 03:45 AM UTC

Hi Matej, 

Greetings from Syncfusion. 

Query: Blazor Grid, with EF and IQueriable data 

We have validated your query and checked the reported problem at our end. We could not able to reproduce the problem at our end while navigating to other page. Find the below sample for your reference. 


Note: Before running the sample change the database file path in OrderContext.cs file in Data folder 

Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 



MG Matej Golob October 1, 2021 06:26 AM UTC

Hi. Thanks for reply. Attached example uses CustomAdaptor. This works well also for me.

Problem is that it doesn't work well if I attach grid directly to IQueryable datasource.



RN Rahul Narayanasamy Syncfusion Team October 4, 2021 02:26 PM UTC

Hi Matej, 

Thanks for the update. 

We are currently checking the reported query with the provided details and we will update the further details in two business days. Until then we appreciate your patience. 

Regards, 
Rahul 



RN Rahul Narayanasamy Syncfusion Team October 6, 2021 05:15 PM UTC

Hi Matej, 

Thanks for your patience. 

We have validated your query and we are able to reproduce the reported case while binding the IQueryable datasource directly to the DataSource property. This is because we have used async operation at our end. So one operation starts before the completion of first operation. We are currently checking the reported case at our end and we will update the further details in two business days. 

Until then we suggest you to resolve the problem by calling Task.Yield() in OnActionBegin event of the Grid. Find the below code snippets and sample for your reference. 

 
    <SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" AllowSorting="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update"})"> 
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
        <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
        <GridPageSettings PageSize="5"></GridPageSettings> 
        <GridColumns> 
            . . . 
        </GridColumns> 
    </SfGrid> 
 
@code{ 
    public IQueryable<Order> Orders { get; set; } 
    public IQueryable<Order> NewOrders { get; set; } 
    SfGrid<Order> Grid; 
    OrderDataAccessLayer db = new OrderDataAccessLayer(); 
    protected override void OnInitialized() 
    { 
        Orders = db.GetAllOrders().AsQueryable(); 
 
    } 
    public async Task ActionBeginHandler(ActionEventArgs<Order> Args) 
    { 
        await Task.Yield(); 
    } 
    } 



Please let us know if you have any concerns. 

Regards, 
Rahul 
 



RC Rogerio C Mauri October 12, 2021 10:49 AM UTC

I'm also having the problem and I need to bind an IQueryable as a data source in the grid.



RN Rahul Narayanasamy Syncfusion Team October 13, 2021 03:31 PM UTC

Hi Rogerio, 
 
Greetings from Syncfusion. 
 
We have validated your query and could you please confirm that you have faced the same problem as mentioned above in this forum. Whether did you facing the same exception while navigating to another page when binding IQueryable datasource to directly to the Grid Datasource directly. 
 
Could you please confirm your scenario. If the above one was not your scenario , then could you please more details about your scenario. 
 
Regards, 
Rahul 
 
 



CB Craig Boucher October 17, 2021 07:05 PM UTC

I'm evaluating the Syncfusion Blazor controls and also experiencing this issue.  This initial load works but if I click on a column to re-sort I get the "A second operation was started..." error (even with the await Task.Yield() in OnActionBegin).  I also noticed through sql profiler that when loading the page the same query was executed six times.  Please keep us posted on any resolutions/fixes. 

Thanks,

Craig  



RS Renjith Singh Rajendran Syncfusion Team October 18, 2021 11:58 AM UTC

Hi Craig, 

We checked this scenario by using the sample attached from our previous update. But when using the suggested solution(Task.Yield in OnActionBegin event handler) we could not reproduce the reported exception. Sorting works fine with the sample from our side. We are attaching the sample and video for your reference, please download the attachments from the link below, 
 
Kindly refer the above sample, and if you are still facing difficulties then the following details would be helpful for us to proceed further. 

  1. Share the exact scenario or replication procedure to reproduce the reported problem.
  2. Share a simple issue reproducing sample for us to validate. Or if possible reproduce the problem with the attached sample and share with us for further analysis.
  3. Share a video demo showing the replication of the problem you are facing.
  4. Share the complete details of exception or error you are facing.
  5. Share the complete Grid rendering codes and the details of the DataSource you are using for Grid.

The provided information will help us analyze the problem, and provide you a solution as early as possible. 

Regards, 
Renjith R 


Loader.
Up arrow icon