2011-08-05 72 views
2

我出現以下情況例外,當我運行在行異常中的TransactionScope

EntityScope<TQFormContext>.CurrentObjectContext.SaveChanges(); 

下面的代碼「與當前連接關聯的事務已完成,但還沒有被釋放。該交易必須在連接前佈置可以用來執行SQL語句。「

[TestMethod()] 
    public void TransactionTest() 
    { 
     using (TransactionScope tc = new TransactionScope()) 
     { 

      var u1 = EntityScope<TQFormContext>.CurrentObjectContext.ASUsers.FirstOrDefault(u => u.UserID == 1); 

      var u2 = PersistenceManager.GetById<TQ.Business.PO.Administration.UserPO>(2); 
      u1.MiddleInitial = "1"; 
      u2.MiddleInitial = "1"; 

      using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
      { 
       SqlCommand sqlComm = new SqlCommand("Update asusers set middleinitial='1' where userid=3", sqlConn); 
       sqlConn.Open(); 
       sqlComm.ExecuteNonQuery(); 
      } 

      PersistenceManager.SaveOrUpdate(u2); 
      EntityScope<TQFormContext>.CurrentObjectContext.SaveChanges(); 

     // throw new Exception("Blah"); 
      tc.Complete(); 
     } 
    } 

回答

0

默認的事務範圍選項是必需的,這將使你的新的TransactionScope加入現有的環境transacton。所以我認爲如果你在構造函數中通過了RequiresNew Transaction Scope選項,這應該可以工作。

相關問題