LINQ to SQL更新后返回旧数据 [英] LINQ to SQL returning old data after an update

查看:65
本文介绍了LINQ to SQL更新后返回旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用LINQ to SQL连接到数据库的应用程序.我在将LINQ转换为SQL以返回正确的新更新数据时遇到问题.

I have an app that uses LINQ to SQL to connect to database. I have a problem getting LINQ to SQL to return correct new update data.

发生的事情是,我将UI,LINQ上的一个字段更改为SQL生成的更新语句,并将新数据存储在数据库中.但是,LINQ to SQL一直返回旧数据,之后,我必须停止并重新启动IIS以获取新的更新数据.

What happened is that, I changed a field on UI, LINQ to SQL generated update statement, and stored the new data in database. However, LINQ to SQL keeps returning the old data after that, I have to stop and restart IIS to get the new updated data.

更新

请注意,旧数据是从LINQ返回到SQL的.

Please note that the old data is returned from LINQ to SQL.

有什么主意吗?

推荐答案

由于Linq to SQL处理缓存的方式,有两种情况可能导致异常行为:

Because of the way Linq to SQL handles caching, there are two situations that can cause odd behavior:

  1. DataContext的重用. DataContext是用于一个工作单元"的,其中工作单元"本质上是您使DataContext生效所能花费的最短时间.请参阅 MSDN页面上的备注"部分.
  2. 多个DataContext s.它们将维护单独的缓存,因此使用一个DataContext进行的更改可能不会反映在从其他DataContext检索的数据中.您可以通过设置ObjectTrackingEnabled关闭缓存,这将通过蛮力解决问题.否则,将在此处介绍一些用于管理并发DataContexts的技术:如何:管理更改冲突
  1. Reuse of DataContext. DataContext is meant to be used for one "unit of work", where a "unit of work" is essentially the shortest amount of time you can get away with letting the DataContext live. See the Remarks section on the MSDN page.
  2. Multiple DataContexts. They will maintain separate caches, so changes made using one DataContext may not be reflected in data being retrieved from others. You can turn off caching by setting ObjectTrackingEnabled, which will brute-force fix the issue. Otherwise, some techniques for managing concurrent DataContexts are described here: How to: Manage Change Conflicts

这篇关于LINQ to SQL更新后返回旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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