/// <summary> /// Enlists this <see cref="T:System.Data.Entity.Core.EntityClient.EntityConnection" /> in the specified transaction. /// </summary> /// <param name="transaction">The transaction object to enlist into.</param> /// <exception cref="T:System.InvalidOperationException"> /// The state of the <see cref="T:System.Data.Entity.Core.EntityClient.EntityConnection" /> is not /// <see /// cref="F:System.Data.ConnectionState.Open" /> /// . /// </exception> public override void EnlistTransaction(Transaction transaction) { if (_storeConnection == null) { throw Error.EntityClient_ConnectionStringNeededBeforeOperation(); } if (State != ConnectionState.Open) { throw Error.EntityClient_ConnectionNotOpen(); } try { var interceptionContext = new EnlistTransactionInterceptionContext(InterceptionContext); interceptionContext = interceptionContext.WithTransaction(transaction); DbInterception.Dispatch.Connection.EnlistTransaction(_storeConnection, interceptionContext); // null means "Unenlist transaction". It is fine if no transaction is in progress (no op). Otherwise // _storeConnection.EnlistTransaction should throw and we would not get here. Debug.Assert( transaction != null || !EnlistedInUserTransaction, "DbConnection should not allow unenlist from a transaction that has not completed."); // It is OK to enlist in null transaction or multiple times in the same transaction. // In the latter case we don't need to be called multiple times when the transaction completes // so subscribe only when enlisting for the first time. Note that _storeConnection.EnlistTransaction // will throw in invalid cases (like enlisting the connection in a transaction when another // transaction has not completed) so when we get here we are sure that either no transactions are // active or the transaction the caller tries enlisting to // is the active transaction. if (transaction != null && !EnlistedInUserTransaction) { transaction.TransactionCompleted += EnlistedTransactionCompleted; } _enlistedTransaction = transaction; } catch (Exception e) { if (e.IsCatchableExceptionType()) { throw new EntityException(Strings.EntityClient_ProviderSpecificError(@"EnlistTransaction"), e); } throw; } }