title: Entity Framework Transaction With Multiple Threads
body: <p>I have an application running multiple threads. The threads do NOT share an ObjectContext (each thread has its own - I know they are not thread safe). </p>
  
  <p>However, the threads are all operating under a shared Transaction. The original thread creates a TransactionScope and each thread it spawns creates a TransactionScope using a DependentTransaction from the Transaction on the main thread.</p>
  
  <p>When multiple ObjectContext requests run at the same time, I sometimes (not consistently) get the error:</p>
  
  <pre><code>System.Data.EntityException occurred
    Message=An error occurred while closing the provider connection. See the inner exception for details.
  
    InnerException: System.Transactions.TransactionException
         Message=The operation is not valid for the state of the transaction.
         Source=System.Transactions
         StackTrace:
              at System.Transactions.TransactionStatePSPEOperation.get_Status(InternalTransaction tx)
              at System.Transactions.TransactionInformation.get_Status()
              at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
              at System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
              at System.Data.SqlClient.SqlConnection.Close()
              at System.Data.EntityClient.EntityConnection.StoreCloseHelper()
         InnerException: 
  </code></pre>
  
  <p>I only know they are running at the same time because when I run my unit tests in debug mode and this exception pops up, if I look at the different threads that are running, I always see at least one other thread halted at an ObjectContext operation.</p>
  
  <p>Also, after doing some reading, I tried adding <code>multipleactiveresultsets=False</code> to my connection string and that made no difference.</p>
  
  <p>Is this a bug in Entity Framework?</p>