title: Call stored procedure inside transaction using Entity Framework
body: <p>I am trying to run the following transaction using Entity Framework. Inside transaction scope I call stored procedure from the DB.</p>
  
  <pre><code> using (mother_Entities entitiesContext = context.Value)
   {
      using (var transactionScope = new TransactionScope())
      {   
          // a lot of create, insert, update operations goes here
          ...
          entitiesContext.SaveChanges();
  
          //Execute stored procedure:
          var paramMessage = new ObjectParameter("MESSAGE", "");
          var paramMotherid = new ObjectParameter("MOTHERID", motherProductId);
          var paramTochteridlist = new ObjectParameter("TOCHTER_ID_LIST", string.Join(";", motherIds));
          var paramError = new ObjectParameter("ERROR", typeof(int));
          var paramErrorText = new ObjectParameter("ERR_TEXT", typeof(string));
  
          entitiesContext.ExecuteFunction("SP_DOCUWARE_UPDATE", paramMessage, paramMotherid,
                                              paramTochteridlist, paramError, paramErrorText);
  
          ...
          transactionScope.Complete();
       }
   }
  </code></pre>
  
  <p>On the line <code>entitiesContext.ExecuteFunction()</code> I get exception <code>Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0</code></p>
  
  <p>My stored procedure doesn't use any transactions and doesn't call any others functions or procedures. So I don't understand why I can't execute strored procedure inside transaction.</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Oh, I found this in the stored procedure:</p>
  
  <pre><code>...
      IF @COMMIT = 1
      BEGIN
          IF @CANCEL = 1 
              ROLLBACK
          ELSE
              COMMIT
      END
      ELSE IF @CHECK = 1
          ROLLBACK
      END
  ...
  </code></pre>
  
  <p>May be after commit exception is thrown. But how to escape this error?</p>