出错代码段:
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