MVC3"不允许启动新事务,因为有其他线程正在该会话中运行"错误解决方法
生活随笔
收集整理的這篇文章主要介紹了
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; }不要懷疑事情是否會正常更新,系統很智能測試結果顯示很正常!沒有出錯也沒有數據錯亂的問題!
轉載于:https://www.cnblogs.com/firstcsharp/archive/2013/05/16/3081806.html
總結
以上是生活随笔為你收集整理的MVC3"不允许启动新事务,因为有其他线程正在该会话中运行"错误解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4种基站相关概念——宏基站、微基站、皮基
- 下一篇: iPhone 4如何流畅运行iOS 7的