title: Proper way of using BeginTransaction with Dapper.IDbConnection
body: <p>Which is the proper way of using <code>BeginTransaction()</code> with <code>IDbConnection</code> in Dapper ?</p>
  
  <p>I have created a method in which i have to use <code>BeginTransaction()</code>. Here is the code.</p>
  
  <pre><code>using (IDbConnection cn = DBConnection)
  {
      var oTransaction = cn.BeginTransaction();
  
      try
      {
          // SAVE BASIC CONSULT DETAIL
          var oPara = new DynamicParameters();
          oPara.Add("@PatientID", iPatientID, dbType: DbType.Int32);
          ..........blah......blah............
      }
      catch (Exception ex)
      {
          oTransaction.Rollback();
          return new SaveResponse { Success = false, ResponseString = ex.Message };
      }
  }
  </code></pre>
  
  <p>When i executed above method - i got an exception -</p>
  
  <blockquote>
    <p>Invalid operation. The connection is closed.</p>
  </blockquote>
  
  <p>This is because you can't begin a transaction before the connection is opened. So when i add this line: <code>cn.Open();</code>, the error gets resolved. But i have read somewhere that manually opening the connection is <strong>bad practice!!</strong> Dapper opens a connection only when it needs to.</p>
  
  <p>In Entity framework you can handle a transaction using a <code>TransactionScope</code>.</p>
  
  <p>So my question is what is a good practice to handle transaction without adding the line <code>cn.Open()...</code> in <strong>Dapper</strong> ? I guess there should be some proper way for this.</p>