title: EF6 Nested Transactions
body: <p>I have a command service class, which utilises the unit of work pattern, with various methods to update a database (SQL Azure, in this case) via entity framework.</p>
  
  <p>The command service gets instantiated with a reference to an instance of a dbcontext, whose lifetime is managed by my DI framework of choice.</p>
  
  <p>Some of the command service class' methods wraps multiple updates to the database within a transaction, for example:</p>
  
  <pre><code>public void UpdateStuff(someEntity)
  {
      using(var tx = _db.Database.BeginTransaction())
      {
          //Some updates to db
          _db.SaveChanges();
          //Some other updates to db
          _db.SaveChanges();
          tx.Commit();
      }
  }
  </code></pre>
  
  <p>Now, some of these methods calls other methods of the command class from within their transactions, for instance:</p>
  
  <pre><code>public void UpdateWithSomeCascadingStuff(someOtherEntity)
  {
      using(var tx = _db.Database.BeginTransaction())
      {
          //Some updates to db
          _db.SaveChanges();
  
          //Some other cascading logic and updates to db
          var relatedEntityToUpdate = _query.GetSomeEntityToUpdate(someOtherEntity);
          UpdateStuff(relatedEntityToUpdate);
          _db.SaveChanges();
          tx.Commit();
      }
  }
  </code></pre>
  
  <p>Clearly, by doing this, I am nesting EF transactions for the same DbContext instance.</p>
  
  <p>Is this supported and will it cause any trouble?  Are there any alternative approaches I can take?</p>
  
  <p>UPDATE:
  I am using EF6 Code First</p>