title: Why doesn't TransactionScope work with Entity Framework?
body: <p>See the code below.  If I initialize more than one entity context, then I get the following exception on the <strong>2nd set of code only</strong>.  If I comment out the second set it works.</p>
  
  <blockquote>
    <p>{"The underlying provider failed on Open."}</p>
    
    <p>Inner: {"Communication with the underlying transaction manager has failed."}</p>
    
    <p>Inner: {"Error HRESULT E_FAIL has been returned from a call to a COM component."}</p>
  </blockquote>
  
  <p>Note that this is a sample app and I know it doesn't make sense to create 2 contexts in a row.  However, the production code does have reason to create multiple contexts in the same <code>TransactionScope</code>, and this cannot be changed.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is a previous question of me trying to set up MS-DTC.  It seems to be enabled on both the server and the client.  I'm not sure if it is set up correctly.  Also note that one of the reasons I am trying to do this, is that existing code within the <code>TransactionScope</code> uses ADO.NET and Linq 2 Sql... I would like those to use the same transaction also.  (That probably sounds crazy, but I need to make it work if possible).</p>
  
  <p><a href="https://stackoverflow.com/questions/794364/how-do-i-use-transactionscope-in-c">How do I use TransactionScope in C#?</a></p>
  
  <p><strong>Solution</strong></p>
  
  <p><em>Windows Firewall was blocking the connections to MS-DTC.</em></p>
  
  <pre><code>using(TransactionScope ts = new System.Transactions.TransactionScope())
          {
                  using (DatabaseEntityModel o = new DatabaseEntityModel())
                  {
                      var v = (from s in o.Advertiser select s).First();
                      v.AcceptableLength = 1;
                      o.SaveChanges();
                  }
  
                  //-&gt; By commenting out this section, it works
                  using (DatabaseEntityModel o = new DatabaseEntityModel())
                  {
                      //Exception on this next line
                      var v = (from s1 in o.Advertiser select s1).First();                         v.AcceptableLength = 1;
                      o.SaveChanges();
                  }
                  //-&gt;
  
                  ts.Complete();
          }
  </code></pre>