title: Single transaction over multiple contexts in Entity Framework 6
body: <p>We have a scenario to save two entities from two contexts in single transaction.</p>
  
  <p>Step 1 -  SetTransaction(firstContext, true);</p>
  
  <p>Step 2 -  Save first entity using firstContext.</p>
  
  <p>Step 3 -  SetTransaction(secondContext, false); </p>
  
  <p>Step 4 -  Save second entity using secondContext</p>
  
  <p>Step 5 -  finally commit the transaction.</p>
  
  <pre><code>void function SetTransaction(context, startNewTransaction)
  {    
     var currentContext = firstContext;
  
     if (startNewTransaction)
     {
        var connection = currentContext.GetConnection();
        connection.Open();
        this.dbTransaction = connection.BeginTransaction();
     }
  
     if (this.dbTransaction != null)
     {
         currentContext.UseTransaction(dbTransaction);
     }
  }
  </code></pre>
  
  <p>While executing Step 3, currentContext.UseTransaction(dbTransaction); line throws the exception as  "<strong>The transaction passed in is not associated with the current connection. Only transactions associated with the current connection may be used</strong>"</p>
  
  <p>Please suggest how to resolve.</p>
  
  <p>Venkat.</p>