title: Entity framework Core ObjectDisposeException: "Safe handle has been closed"
body: <h2>Setup</h2>
  
  <p>Entity framework core 1.1</p>
  
  <p>WPF application .NET 4.5.2</p>
  
  <h2>The Problem:</h2>
  
  <p>I am sometimes encountering this exception while running a query on a sqlite database.</p>
  
  <blockquote>
    <p>System.ObjectDisposedException occurred   HResult=0x80131622<br>
    Message=Safe handle has been closed   Source=mscorlib   StackTrace:<br>
    at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean&amp;
    success)    at
    System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle,
    Boolean&amp; 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()<br>
    at
    Microsoft.EntityFrameworkCore.Storage.Internal.SqliteRelationalConnection.Close()
    at
    Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.Dispose()
    at
    Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.&lt;_ShapedQuery>d__3<code>1.MoveNext()
    at System.Linq.Enumerable.WhereSelectEnumerableIterator</code>2.MoveNext()<br>
    at
    Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor<code>1.EnumeratorExceptionInterceptor.MoveNext()
    at System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1 collection)<br>
    at System.Linq.Enumerable.ToList[TSource](IEnumerable</code>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)<br>
    at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object
    argument)</p>
  </blockquote>
  
  <p>The query which I am running inside a loop with a new context created just before the loop.</p>
  
  <pre><code>DatabaseContext.Devices.Load();
  var deviceFrom =
      DatabaseContext.SocketSockets.AsNoTracking()
          .Include(i =&gt; i.SocketFrom)
          .Where(ss =&gt; ss.SocketTo.PointNameId == pointName.PointNameId)
          .Select(s =&gt; s.SocketFrom)
          .Where(s =&gt; s.IsConnected || s.IsSupplyOrGround == false)
          .Select(s =&gt; s.Device)
          .ToList();
  
  var deviceTo =
      DatabaseContext.SocketSockets.AsNoTracking()
          .Include(i =&gt; i.SocketTo)
          .Where(ss =&gt; ss.SocketFrom.PointNameId == pointName.PointNameId)
          .Select(s =&gt; s.SocketTo)
          .Where(s =&gt; s.IsConnected || s.IsSupplyOrGround == false)
          .Select(s =&gt; s.Device)
          .ToList();
  </code></pre>
  
  <p>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?</p>
  
  <p>Additionally, my <code>SocketSocket</code> table is basically a many-to-many mapping which contains <code>Socket</code> pairs.</p>
  
  <p>The problem does not happen all the time at the same spot, even with the same data. </p>
  
  <ul>
  <li>Could this exception be caused by trying to have multiple context accessing the same sqlite database at the same time?</li>
  </ul>