LINQ没有显示与数据库相同的数据 [英] LINQ not showing same data as database

查看:61
本文介绍了LINQ没有显示与数据库相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的问题。  我不确定为什么会这样。  我有一个LINQ-to-SQL数据上下文,我创建了一些异步运行的代码来更新表。

I have an interesting problem.  I'm not sure why it's happening.  I have a LINQ-to-SQL data context and I created some code that runs asynchronously to update the table.

在异步代码中,我创建一个新的datacontext并传入ID而不是实体。  然后我在新的datacontext中加载实体,更新值,提交更改然后重新查询表单。  它似乎成功了,但是这个值在LINQ数据上下文中没有改变

In the async code, I create a new datacontext and pass in the ID instead of the entity.  Then I load the entity in the new datacontext, update the value, submit the changes then requery the form.  It appears to succeed, but the value doesn't change in the LINQ data context.

现在,这里变得奇怪了。  当我加载SQL数据库并运行select查询时,值IS已更新。  当我在表单中重新加载它时,它表示它是旧的值。

Now, here's where it gets weird.  When I load the SQL database and run a select query the value IS updated.  When I load it back up in the form, it says it's the old value.

这是它变得怪异的地方。  当我重新启动应用程序时,它仍然使用旧值。  这就像是陷入了交易,但我没有使用任何交易范围。  即便如此,为什么数据库会说它已被更新?

Here's where it gets weirder.  When I restart the application, it's STILL using the old value.  It's like it's caught up in a transaction, but I'm not using any transaction scope.  And even then, why would the database say it's been updated?

新实体,新数据上下文,旧值。  这有什么意义?

New entity, new data context, old value.  How does this make sense?

 

以下是代码: (它在backgroundworker runasync事件中运行)

Here's the code:  (it runs in backgroundworker runasync event)


public void StartReceive(List<POReceiveItem> items)
    {
      try
      {
        LDInventoryDataContext newLDC = new LDInventoryDataContext(ClassPOSSession.Current.ConnectionString);

        if (items != null)
        {
          foreach (POReceiveItem item in items)
          {
            PurchaseOrderDetail poDetail = newLDC.PurchaseOrderDetails.SingleOrDefault(d => d.PODetailID == item.PODetailID);
            BranchInventory bri = newLDC.BranchInventories.SingleOrDefault(d => d.InventoryID == item.InventoryID && d.BranchID == item.BranchID);

            if(poDetail != null)
              poDetail.ReceivedQty += item.Receive;
            
            if(bri != null)
              bri.InStock += item.Receive;
          }

          newLDC.SubmitChanges();
        }
        
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error processing: " + ex.Message);
      }
    }

推荐答案

Hello Railu,

Hello Railu,

 

欢迎来到MSDN论坛,感谢您在此发帖。

Welcome to the MSDN Forum and thanks for posting here.

根据您的描述,我认为有一个原因。也许您使用旧的程序集,所以请尝试执行以下操作?

According to your description, I think there is a reason. Maybe you used the old assmblies, so could you please try to perform as below?

右键单击解决方案并选择"清理",然后构建项目。这可能是正常的。

Right click the solution and choose "clean", then build your project. It might be normal.

如果您有任何问题,请随时告诉我。

Please feel free to let me know if you have any questions.

 

祝你有个美好的一天,


这篇关于LINQ没有显示与数据库相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆