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.
请问怎么回事,求指点,跪求啊
仿照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()) {
......
确实是延迟的问题,要么关闭延迟,要么执行后在关闭事物