Disposing issue when trying to load data from SQLite database
-
I'm using Syncfusion in my WPF MVVM project. I want to add pagination to my datagrid. The following code can do this:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
using (var _sqliteContext = new SQLiteContext())
{
dataPager.LoadDynamicItems(args.StartIndex, _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize));
//resetting cache for all pages.
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}
}The problem is that when I run the code, the application falls into the break mode and says:
Quote:
System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing of a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'SQLiteContext'.'
How can I fix it?
-
I'm using Syncfusion in my WPF MVVM project. I want to add pagination to my datagrid. The following code can do this:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
using (var _sqliteContext = new SQLiteContext())
{
dataPager.LoadDynamicItems(args.StartIndex, _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize));
//resetting cache for all pages.
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}
}The problem is that when I run the code, the application falls into the break mode and says:
Quote:
System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing of a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'SQLiteContext'.'
How can I fix it?
-
Code4Ever wrote:
This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement.
Try reworking without the
using
statement.Thanks. Solved using the following code rework:
private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
List data;
using (var _sqliteContext = new SQLiteContext())
{
data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize).ToList();
}
dataPager.LoadDynamicItems(args.StartIndex, data);
//resetting cache for all pages.
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}