Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL returning old data after an update

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.

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.

Update

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

Any idea?

like image 776
Pingpong Avatar asked Feb 21 '26 11:02

Pingpong


1 Answers

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

  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
like image 143
Sean U Avatar answered Feb 22 '26 23:02

Sean U