LINQ Query Returning Old Data After Update

Sometimes linq query returns old data after update. This is because of data context internal caching.
Follow this link to understand more about data context.
Now the question is how to avoid this, below i have listed couple of techniques to avoid data context internal caching and force linq to reload results from database.

1) If you set ObjectTrackingEnabled to false, the DataContext does not track the object.

2) The second object is to refresh the object to database values after it has returned the object.
For example
var cacheddate = (from d in DataContextObj.users where username = "unique" && active == "true" select d).firstordefault();

Now after performing some update, the data is updated but query still returns old data. In this case you can refresh your object (in our case it is "cacheddata" ) to database values.
You should refresh thiis variable just after executing a query, see below for example


var cacheddate = (from d in DataContextObj.users where username = "unique" && active == "true" select d).firstordefault();
DataContextObj.Refresh(RefreshMode.OverwriteCurrentValues, cacheddate);

You can always ask in comments if there is any question. 

Thanks


Comments

Popular posts from this blog

GRANDING UTime Master - IDOR (CVE-2023-45393)

GRANDING UTime Master - Stored XSS (CVE-2023-45391)

Ericsson BSCS iX R18 Billing & Rating (ADMX, MX) - Stored XSS (CVE-2020-29144, CVE-2020-29145)