LINQ to SQL更新后返回旧数据 [英] LINQ to SQL returning old data after an update
问题描述
我有一个使用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:
-
DataContext
的重用.DataContext
是用于一个工作单元"的,其中工作单元"本质上是您使DataContext
生效所能花费的最短时间.请参阅 MSDN页面上的备注"部分. - 多个
DataContext
s.它们将维护单独的缓存,因此使用一个DataContext
进行的更改可能不会反映在从其他DataContext
检索的数据中.您可以通过设置ObjectTrackingEnabled
关闭缓存,这将通过蛮力解决问题.否则,将在此处介绍一些用于管理并发DataContexts
的技术:如何:管理更改冲突
- 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 theDataContext
live. See the Remarks section on the MSDN page. - Multiple
DataContext
s. They will maintain separate caches, so changes made using oneDataContext
may not be reflected in data being retrieved from others. You can turn off caching by settingObjectTrackingEnabled
, which will brute-force fix the issue. Otherwise, some techniques for managing concurrentDataContexts
are described here: How to: Manage Change Conflicts
这篇关于LINQ to SQL更新后返回旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!