title: Managing transactions between EntityFramework and EnterpriseLibrary's DatabaseFactory
body: <p>I'm working with an existing set of code that manages multiple database updates in a single transaction. Here is a simplified example: </p>
  
  <pre><code>Database db = DatabaseFactory.CreateDatabase();
  using (DbConnection dbConnection = db.CreateConnection())
  {
      dbConnection.Open();
      DbTransaction dbTransaction = dbConnection.BeginTransaction();
      try
      {
          //do work
          dbTransaction.Commit();
      }
      catch (Exception ex)
      {
          dbTransaction.Rollback();
      }
  }
  </code></pre>
  
  <p>I am also using EntityFramework in this same project for new development. Below is a simplified example of the usage of my repository class:</p>
  
  <pre><code>List&lt;ThingViewModel&gt; things = new List&lt;ThingViewModel&gt;();
  // populate list of things
  IObjectRepository thingRepository = new ThingRepository();
  thingRepository.AddThings(things);
  thingRepository.Save();
  </code></pre>
  
  <p><strong>I want the work done in 'AddThings' to happen as part of the transaction in the first block of code.</strong></p>
  
  <p>Is there some clean way of blending my repository pattern into this existing code or vice-versa? I'm not at the point yet where it is feasible to rewrite the existing code to be entirely within EntityFramework, so I'm looking for some some interim approach. </p>
  
  <p>I have tried passing the transaction from the older code into the repository, and thus EntityFramework, but that does not seem to work. I have also tried passing the ObjectContext back out to the older code in order to enlist it in the transaction. Neither approach works.</p>
  
  <p>I cannot believe that I am the first person to encounter this hurdle in migrating existing code to EntityFramework... there must be something I am not considering.</p>
  
  <p>I'll list the things that I have tried below:</p>
  
  <pre><code>using (TransactionScope transactionScope = new TransactionScope())
  {   
      Database db = DatabaseFactory.CreateDatabase();
      using (DbConnection dbConnection = db.CreateConnection())
      {
          dbConnection.Open();
          DbTransaction dbTransaction = dbConnection.BeginTransaction();
          try
          {
              //do work
              dbTransaction.Commit();
          }
          catch (Exception ex)
          {
              dbTransaction.Rollback();
          }
      }
  
      Thing thing = new Thing(){
          Prop1 = Val1,
          Prop2 = Val2
      };
      ThingObjectContext context = new ThingObjectContext();
      context.Things.AddObject(thing);
      context.SaveChanges();
      transactionScope.Complete();
  }
  </code></pre>
  
  <p>This last example 'works', it does not function as a transaction. When the EF insert fails, the EL commands are not rolled back by the TransactionScope. If I don't put those explicit calls to .Commit() and .SaveChanges(), nothing happens. I would really like for this to share the same connection if possible. Two other variations of this I am currently playing around with is trying to use the same connection between both EF and EL as well as use EnlistTransaction on one side or the other. Definitely trying to keep this from becoming a MSDTC - don't want the extra overhead associated with that.</p>