Entity framework Core ObjectDisposeException: "Safe handle has been closed"

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?
sqlite
entity-framework-core
objectdisposedexception
asked on Stack Overflow Apr 7, 2017 by ajr

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0