0

Setup

Entity framework core 1.1

WPF application .NET 4.5.2

The Problem:

I am sometimes encountering this exception while running a query on a sqlite database.

System.ObjectDisposedException occurred HResult=0x80131622
Message=Safe handle has been closed Source=mscorlib StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success) at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success) at Microsoft.Data.Sqlite.Interop.NativeMethods.Sqlite3_sqlite3.sqlite3_db_filename(Sqlite3Handle db, IntPtr zDbName) at Microsoft.Data.Sqlite.Interop.NativeMethods.Sqlite3_sqlite3.db_filename(Sqlite3Handle db, IntPtr zDbName) at Microsoft.Data.Sqlite.Interop.NativeMethods.sqlite3_db_filename(Sqlite3Handle db, String zDbName) at Microsoft.Data.Sqlite.Interop.VersionedMethods.Strategy3_7_10.GetFilename(Sqlite3Handle db, String zDbName) at Microsoft.Data.Sqlite.Interop.VersionedMethods.GetFilename(Sqlite3Handle db, String zDbName) at Microsoft.Data.Sqlite.SqliteConnection.get_DataSource() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Close()
at Microsoft.EntityFrameworkCore.Storage.Internal.SqliteRelationalConnection.Close() at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.Dispose() at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__31.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at FxConnection.Persistence.Repositories.PointNameRepository.GetConnectedDeviceNoTracking(PointName pointName) in C:\Users*\documents\visual studio 2017\Projects\project\project\Persistence\Repositories\PointNameRepository.cs:line 357 at FxConnection.ViewModels.ProjectDeviceDatabaseViewModel.InitialLoading_DoWork(Object sender, DoWorkEventArgs e) in C:\Users*\documents\visual studio 2017\Projects\project\project\ViewModels\ProjectDeviceDatabaseViewModel.cs:line 1729 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

The query which I am running inside a loop with a new context created just before the loop.

DatabaseContext.Devices.Load();
var deviceFrom =
    DatabaseContext.SocketSockets.AsNoTracking()
        .Include(i => i.SocketFrom)
        .Where(ss => ss.SocketTo.PointNameId == pointName.PointNameId)
        .Select(s => s.SocketFrom)
        .Where(s => s.IsConnected || s.IsSupplyOrGround == false)
        .Select(s => s.Device)
        .ToList();

var deviceTo =
    DatabaseContext.SocketSockets.AsNoTracking()
        .Include(i => i.SocketTo)
        .Where(ss => ss.SocketFrom.PointNameId == pointName.PointNameId)
        .Select(s => s.SocketTo)
        .Where(s => s.IsConnected || s.IsSupplyOrGround == false)
        .Select(s => s.Device)
        .ToList();

I am wondering if I am doing the query in the right way? Because sometimes it is entirely possible that there are no results. Might this be a problem?

Additionally, my SocketSocket table is basically a many-to-many mapping which contains Socket pairs.

The problem does not happen all the time at the same spot, even with the same data.

  • Could this exception be caused by trying to have multiple context accessing the same sqlite database at the same time?
ajr
  • 874
  • 2
  • 13
  • 29
  • Looks like there are multiple BackgroundWorkers running. – Gert Arnold Apr 07 '17 at 07:43
  • 1
    To elaborate on @GertArnold's comment, It's not that you have multiple contexts - multiple contexts for concurrent queries should work fine - but Entity Framework does not support parallel or concurrent queries using the same `DbContext` instance. See answers [here](http://stackoverflow.com/a/43497310/2319351), [here](http://stackoverflow.com/a/24702369/2319351) and [here](http://stackoverflow.com/a/12827806/2319351) among others. – Oskar Lindberg Apr 19 '17 at 14:05

0 Answers0