title: What are the differences between calling DbConnection.EnlistTransaction and setting DbCommand.Transaction?
body: <p>I am trying to understand the difference between:</p>
  <ul>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbconnection.enlisttransaction?view=net-5.0" rel="nofollow noreferrer"><code>DbConnection.EnlistTransaction</code></a> (and <a href="https://docs.microsoft.com/en-us/dotnet/framework/data/transactions/enlisting-resources-as-participants-in-a-transaction" rel="nofollow noreferrer">related bits of info</a>)</li>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbcommand.dbtransaction?view=net-5.0" rel="nofollow noreferrer"><code>DbCommand.Transaction</code></a></li>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbtransaction?view=net-5.0" rel="nofollow noreferrer"><code>DbTransaction</code></a> which is either something you get from calling <a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbconnection.begindbtransaction?view=net-5.0#System_Data_Common_DbConnection_BeginDbTransaction_System_Data_IsolationLevel_" rel="nofollow noreferrer"><code>DbConnection.BeginTransaction</code></a> or <a href="https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transaction.current?view=net-5.0" rel="nofollow noreferrer">Transaction.Current</a></li>
  </ul>
  <p>My understanding so far:</p>
  <ul>
  <li><code>DbConnection.EnlistTransaction</code>: is mostly about making a connection enlisted to a distributed transaction, which inherits from <code>DbTransaction</code>.</li>
  <li><code>DbCommand.Transaction</code>: is to tell in which transaction (again <code>DbTransaction</code>) the command is going to be executed.</li>
  </ul>
  <p>So far, so good, I just quoted what the official documentation is saying.</p>
  <p>Now my I'm wondering how both are combined... meaning:</p>
  <ul>
  <li><code>DbConnection.EnlistTransaction</code>: needs to be performed before or after opening a connection?</li>
  <li><code>DbCommand.Transaction</code>, if the <code>DbConnection</code> is enlisted to a distributed transaction and creates a <code>DbCommand</code>, does it mean that this command directly inherits from the connection enlisted transaction? But then what if another transaction is assigned to command, which Transaction is used upon the command execution?</li>
  </ul>
  <hr />
  <p>EDIT</p>
  <p>About <code>DbConnection.EnlistTransaction</code>: <a href="https://stackoverflow.com/a/43998009/4636721">https://stackoverflow.com/a/43998009/4636721</a></p>
  <p>About <code>DbConnection.BeginTransaction</code>, it just creates a Transaction and call the begin method which just executes (non-query) a <code>BEGIN TRANSACTION</code> statement. Conversely, <code>Transaction.Commit</code>, is sending the <code>COMMIT</code> statement to the backend.</p>
  <p>For the grand sake of simplicity, I'm just using some <code>System.Data.SQLite</code> source code snippets to illustrate what I'm talking about:</p>
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Attempts to start a transaction.  An exception will be thrown if the transaction cannot
  /// be started for any reason.
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;deferredLock&quot;&gt;TRUE to defer the writelock, or FALSE to lock immediately&lt;/param&gt;
  protected override void Begin(bool deferredLock)
  {
    if (this._cnn._transactionLevel++ != 0)
      return;
    try
    {
      using (SQLiteCommand command = this._cnn.CreateCommand())
      {
        if (!deferredLock)
          command.CommandText = &quot;BEGIN IMMEDIATE;&quot;;
        else
          command.CommandText = &quot;BEGIN;&quot;;
        command.ExecuteNonQuery();
      }
    }
    catch (SQLiteException ex)
    {
      --this._cnn._transactionLevel;
      this._cnn = (SQLiteConnection) null;
      throw;
    }
  }
  
  /// &lt;summary&gt;Commits the current transaction.&lt;/summary&gt;
  public override void Commit()
  {
    this.CheckDisposed();
    this.IsValid(true);
    if (this._cnn._transactionLevel - 1 == 0)
    {
      using (SQLiteCommand command = this._cnn.CreateCommand())
      {
        command.CommandText = &quot;COMMIT;&quot;;
        command.ExecuteNonQuery();
      }
    }
    --this._cnn._transactionLevel;
    this._cnn = (SQLiteConnection) null;
  }
  </code></pre>