首页 新闻 搜索 专区 学院

net45 linq 事务 报错, 求指点

0
[已解决问题] 解决于 2013-10-08 21:12
            using (var context = new FfwjdataEntities())
            {
                if(context.Database.Connection.State == System.Data.ConnectionState.Closed){
                    context.Database.Connection.Open();
                }
                using (var dbtran = context.Database.Connection.BeginTransaction())
                {
                    try
                    {
                        #region 一些linq操作。。。
                        #endregion
    
                        dbtran.Commit();
                    }
                    catch(Exception ex)
                    {
                      dbtran.Rollback();
              return;
                    }
                }
            }   

以上是大致代码

 

catch到的Exception:
Exception Details: System.InvalidOperationException: ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.

cathc里的Rollback()抛了异常:This SqlTransaction has completed; it is no longer usable.

请问怎么回事,求指点,跪求啊

Frozen.Zhang的主页 Frozen.Zhang | 初学一级 | 园豆:154
提问于:2013-10-08 19:48
< >
分享
最佳答案
0

仿照net4的事务
其中几行开启事务的语句小修改下就Ok了,谢谢啊

                
          .......
          var connection = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)context).ObjectContext.Connection; if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (var dbtran = connection.BeginTransaction()) {
          ......
Frozen.Zhang | 初学一级 |园豆:154 | 2013-10-08 20:59
其他回答(1)
0

确实是延迟的问题,要么关闭延迟,要么执行后在关闭事物

56180825 | 园豆:1555 (小虾三级) | 2013-10-08 20:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册