System.InvalidOperationException:This.NpgsqlTransaction has completed;it is no longer usable. at Npgsql.NpgsqlTransaction.CheckCompleted() in ...

 关于报此异常,原因是事务不能循环提交,一个方法中事务只能提交一次。

System.InvalidOperationException:This.NpgsqlTransaction has completed;it is no longer usable. at Npgsql.NpgsqlTransaction.CheckCompleted() in ...

  正确使用方法

 using (var pay = _postgreDbContext.Database.BeginTransaction())
            {
                try
                {
                    billItem.review_remark = "通过";
                    foreach (var item in Settlement)
                    {
                        item.status = 2;
                    }
                    _postgreDbContext.SaveChanges();
                    pay.Commit();//注意放到循环外面

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

 

你可能感兴趣的:(System.InvalidOperationException:This.NpgsqlTransaction has completed;it is no longer usable. at Npgsql.NpgsqlTransaction.CheckCompleted() in ...)