Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wrap IDbTransactions in a TransactionScope
    text
    copied!<p>I have several code methods that look like this:</p> <pre><code>using (var connection = this.connectionFactory.GetConnection()) { connection.Open(); using (var transaction = connection.BeginTransaction()) { using (var command = connection.CreateCommand()) { command.Transaction = transaction; command.CommandText = "foo"; command.ExecuteNonQuery(); transaction.Commit(); } } } </code></pre> <p>I now need to call several of these methods together inside an outer transaction, So I did this:</p> <pre><code>using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew)) { method1(); method2(); method3(); } </code></pre> <p>but its doing:</p> <pre><code>The operation is not valid for the state of the transaction. at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction) at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification) at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx) at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx) at System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transaction) at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() </code></pre> <p>Do I need to replace the <code>IDbTransactions</code> with <code>TransactionScopes</code>?</p> <p>What <code>TransactionScopeOption</code> should I use for the outer an inner scopes? Im guessing i want <code>RequiresNew</code> for the outer and <code>Required</code> for the inners?</p> <p>The methods will still be called individually (i.e. without an outer <code>TransactionScope</code> as well as together, so I still need them to be transactionally safe.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload