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();
}
}
I get the following exception when I run the following code at line
EntityScope<TQFormContext>.CurrentObjectContext.SaveChanges();
"The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements."
[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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认的 Transaction Scope 选项是必需的,这将使您的新 TransactionScope 加入现有的环境事务。所以我认为如果您在构造函数中传递 RequiresNew Transaction Scope 选项,这应该可以工作。
The default Transaction Scope option is Required which will make your new TransactionScope join an existing ambient transacton. So I think if you pass the RequiresNew Transaction Scope option in your constructor this should work.