26.1.35 DataManager has InvalidOperationException.

The operation cannot be completed because the DbContext has been disposed error at the line below. This error disappears in  25.2.7 or earlier. I used "Chip" control along with the DataAdapter. Not sure if it is a Chip issue or DataAdapter one.


// Aggregation
        var count = dbContext.Users.Count();


------------------------------------------------------------------

public class UserDataAdapter(AdminDbContext dbContext) : DataAdaptor
{
    public override Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string? additionalParam = null)
    {
        var dataSource = dbContext.Users.AsQueryable<User>();


        // Searching
        if (dataManagerRequest.Search is { Count: > 0 })
        {
            //var matching = await _dbContext.Users.AnyAsync(user => user.Id.Contains(dataManagerRequest.Search[0].Key));
            //if (!matching)
            // return dataSource;
            dataSource = DataOperations.PerformSearching(dataSource, dataManagerRequest.Search);
        }


        // Sorting
        if (dataManagerRequest.Sorted is { Count: > 0 })
            dataSource = DataOperations.PerformSorting(dataSource, dataManagerRequest.Sorted);


        // Filtering
        if (dataManagerRequest.Where is { Count: > 0 })
            dataSource = DataOperations.PerformFiltering(dataSource, dataManagerRequest.Where, dataManagerRequest.Where[0].Operator);


        //Paging
        if (dataManagerRequest.Skip != 0)
            dataSource = DataOperations.PerformSkip(dataSource, dataManagerRequest.Skip);
        if (dataManagerRequest.Take != 0)
            dataSource = DataOperations.PerformTake(dataSource, dataManagerRequest.Take);


        // Aggregation
        var count = dbContext.Users.Count();
        if (dataManagerRequest.Aggregates != null)
        {
            return Task.FromResult<object>(dataManagerRequest.RequiresCounts
                ? new DataResult
                {
                    Result = dataSource,
                    Count = count,
                    Aggregates = DataUtil.PerformAggregation(dataSource, dataManagerRequest.Aggregates)
                }
                : dataSource);
        }


        return Task.FromResult<object>(dataManagerRequest.RequiresCounts
            ? new DataResult() { Result = dataSource, Count = count }
            : dataSource);
    }


    public override async Task<object?> InsertAsync(DataManager dataManager, object record, string additionalParam)
    {
        var user = record as User;
        if (user != null)
        {
            await dbContext.Users.AddAsync(user);
            await dbContext.SaveChangesAsync();


        }
        return user;
    }


    public override async Task<object?> RemoveAsync(DataManager dataManager, object primaryColumnValue, string primaryColumnName, string additionalParam)
    {
        var user = dbContext.Users.FirstOrDefault(user => user.Id == primaryColumnValue.ToString());
        if (user != null)
        {
            dbContext.Users.Remove(user);
            await dbContext.SaveChangesAsync();
        }
        return user;
    }


    public override async Task<object?> UpdateAsync(DataManager dataManager, object record, string primaryColumnName, string additionalParam)
    {
        var user = record as User;
        if (user != null)
        {
            dbContext.Users.Update(user);
            await dbContext.SaveChangesAsync();
        }
        return user;
    }
}

1 Reply

VN Vignesh Natarajan Syncfusion Team June 14, 2024 05:06 AM UTC

Hi Yongkee,


Thanks for contacting Syncfusion support.


Query: “This error disappears in  25.2.7 or earlier. I used "Chip" control along with the DataAdapter. Not sure if it is a Chip issue or DataAdapter one.


Before proceeding further with your requirement, we need some more details about your requirement which will be helpful for us to understand your query and provide solution based on that.


  1. Chip component is not data binding component. So kindly share details how you have define the DataAdaptor to bind data to Chip component
  2. Share the code example of chip and DataManager component.
  3. If possible share the simple issue reproducible sample.
  4. Are you facing the reported issue during the initial rendering itself or while inserting pr removing a chip?
  5. Share the screenshot of entire script error or exception thrown.


Above requested details will be very helpful for us to validate the reported scenario at our end and provide solution as early as possible.


Regards,

Vignesh Natarajan


Loader.
Up arrow icon