开发者

Exception in TransactionScope

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();
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜