title: How use Transaction in EntityFramework 5?
body: <p>I want to trancate some table same time.
  If one not success, must be all rolback.</p>
  
  <p>Something like that:</p>
  
  <pre><code>ctx.Database.ExecuteSqlCommand("truncate table tb_expensesall");
  ctx.Database.ExecuteSqlCommand("truncate table tb_wholesale");
  ctx.Database.ExecuteSqlCommand("truncate table tb_singlesale");
  ctx.Database.ExecuteSqlCommand("truncate table tb_purchase");
  </code></pre>
  
  <p>But the problem is , I dont know how use transaction for this.</p>
  
  <p>I trying this:</p>
  
  <pre><code>using (gasstationEntities ctx = new gasstationEntities(Resources.CONS))
  {
      ctx.Database.Connection.Open();
      DbTransaction tr = ctx.Database.Connection.BeginTransaction();
  
      try
      {
          ctx.Database.ExecuteSqlCommand("truncate table tb_expensesall");
          ctx.Database.ExecuteSqlCommand("truncate table tb_wholesale");
          ctx.Database.ExecuteSqlCommand("truncate table tb_singlesale");
          ctx.Database.ExecuteSqlCommand("truncate table tb_purchase");
          //commit the transaction
          tr.Commit();
          new MessageWindow(this, Resources.GetString("Warn"), Resources.GetString("DeleteSuccess"));
      }
      catch (Exception ex)
      {
          //return
          tr.Rollback();
      }
      //close
      ctx.Database.Connection.Close();
  }
  </code></pre>
  
  <p>The problem here:  <code>tr.Commit();</code>
  and the Exception tell me :</p>
  
  <pre><code>{System.InvalidOperationException: Connection must be valid and open to rollback transaction
  </code></pre>
  
  <p>And the <code>tr.Rollback();</code> throw exception to.
  the exception is:</p>
  
  <pre><code>{System.InvalidOperationException: Connection must be valid and open to rollback transaction
  </code></pre>
  
  <p>The realy funy thing is , the table truncate is success. what? the commmit is throw exception . and it can be success? i can't understand.</p>
  
  <p>Please tel me  what is goning on . if you give me a solution, that's even better.</p>