LINQ强制更新数据
最近一项目里使用LINQ时,我一后台线程修改数据后,前台窗体总是得不到最新的数据。无解。
查看了获取数据的数据层,是NEW的DB应该是最新的,怎么会出现这个问题呢。
百度之后说用DB.REFRESH,强制更新,结果不是不行。我开始怀疑是后台线程的问题,结果果然发现是线程只执行了一次,没有加循环,我勒个去!
- #region 后台作废订单
- private void OrderUpdateThread()
- {
- System.Threading.Thread orderThread = new System.Threading.Thread(CanceledOrders);
- orderThread.Start();
- }
- private void CanceledOrders()
- {
- while (true)
- {
- try
- {
- using (DB.DBJewelryRetailDataContext db = new DBJewelryRetailDataContext())
- {
- List<DB.RetailOrder> orders = db.RetailOrder.Where(f =>
- f.fDate.AddMinutes(30) <= DateTime.Now &&
- f.fGatheringState == Enums.GatheringState.Unreceived&&
- f.fIsDelete == false
- ).ToList();
- DB.InOutVouch inoutVouch = null;
- foreach (DB.RetailOrder retail in orders)
- {
- //作废订单
- retail.fIsDelete = true;
- //作废收款单
- DB.GatheringVouch gv = db.GatheringVouch.FirstOrDefault(f => f.fchrID == retail.fOrderID);
- if (gv != null)
- {
- gv.fIsDeleted = true;
- }
- //删除出入库单,恢复库存
- inoutVouch = db.InOutVouch.FirstOrDefault(f => f.fchrBusinessID == retail.fOrderID);
- db.InOutVouchDetail.DeleteAllOnSubmit(inoutVouch.InOutVouchDetail);
- db.InOutVouch.DeleteOnSubmit(inoutVouch);
- }
- //提交
- db.SubmitChanges();
- //1分钟刷新一次
- System.Threading.Thread.Sleep(60000);
- }
- }
- catch(Exception)
- {
- }
- }
- }
- #endregion
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP