TransactionScope中的异常
本文关键字:异常 TransactionScope | 更新日期: 2023-09-27 18:04:32
当我在
行运行以下代码时,得到以下异常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();
}
}
默认的TransactionScope选项是Required,这将使您的新TransactionScope加入现有的环境事务。所以我认为如果你在构造函数中传递RequiresNew Transaction Scope选项,这应该可以工作。