zoukankan      html  css  js  c++  java
  • MVC3"不允许启动新事务,因为有其他线程正在该会话中运行"错误解决方法

    出错代码段:

     public void UPdateStockForAudit(string ordercode)
            {
                var ditems=_db.Orderdetails.Where(o=>o.OrderCode==ordercode);
                foreach (var item in ditems)
                {
                    decimal inventory= item.PCS * item.Stock.TonPerPCS;
                    UpdateStock(item.StockId, item.PCS,inventory);
                }
            }
            private void UpdateStock(int stockid,int selledpcs,decimal inventory)
            {
                var item=_db.Stocks.SingleOrDefault(s=>s.StockId==stockid);
                item.Selled=selledpcs;
                item.Inventory=inventory;  
    _db.SaveChanges(); }

    修正后的(关键点在于_db.SaveChanges()把它放在循环外面即可! ):

     public void UPdateStockForAudit(string ordercode)
            {
                var ditems=_db.Orderdetails.Where(o=>o.OrderCode==ordercode);
                foreach (var item in ditems)
                {
                    decimal inventory= item.PCS * item.Stock.TonPerPCS;
                    UpdateStock(item.StockId, item.PCS,inventory);
                }
                _db.SaveChanges();
            }
            private void UpdateStock(int stockid,int selledpcs,decimal inventory)
            {
                var item=_db.Stocks.SingleOrDefault(s=>s.StockId==stockid);
                item.Selled=selledpcs;
                item.Inventory=inventory;            
            }

    不要怀疑事情是否会正常更新,系统很智能测试结果显示很正常!没有出错也没有数据错乱的问题!

    学习交流群:364976091
  • 相关阅读:
    Kafka Shell基本命令(包括topic的增删改查)
    thefuck的安装和使用
    Linux运维利器之ClusterShell
    MySQL数据库的10大经典错误案例
    Mysql 常用操作
    Git 忽略特定文件或文件夹
    为什么需要拷贝构造函数
    C语言编译过程
    设计模式之建造者模式
    设计模式之工厂/抽象工厂模式
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/3081806.html
Copyright © 2011-2022 走看看