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
  • 相关阅读:
    UVA 10391 STL容器的使用
    UVA 10763
    UVA 10935
    UVA 洪水
    UVA 1594 set 里面放queue
    关于STL 容器的嵌套使用, 小试牛刀
    丑数 UVA 136
    UVA 1368 DNA
    antd 上传文件控件使用方法(坑)
    mysql查询一条工单时间需要10秒。优化sql语句得以解决。
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/3081806.html
Copyright © 2011-2022 走看看