Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct usage of DataContext.Refresh()?

I have a LinqToSql object in memory, whose field values on the database are expected to change during the lifetime of the object. So periodically I need to check if everything is still in sync. I was expecting to be able to do this like so:

myDataContext.Refresh(RefreshMode.KeepCurrentValues, myObj);

but unfortunately this seems to have no effect; the values on myObj remain the same even when the DB values have changed. MSDN documentation on this method is pretty scant. Can anyone tell me what I am missing here?

like image 758
Shaul Behr Avatar asked Jun 06 '11 15:06

Shaul Behr


2 Answers

If you want to "refresh" your entity with the up-to-date values, then the appropriate mode would be RefreshMode.KeepChanges or RefreshMode.OverwriteCurrentValues.

KeepChanges will leave any locally changed value as is. OverwriteCurrentValues will fetch all values from the database.

Beware of ChangeConflictExceptions.

like image 194
SandRock Avatar answered Nov 15 '22 15:11

SandRock


If you want your refreshed object's current values to match what's currently in the database you'll need to use RefreshMode.OverwriteCurrentValues mode instead.

like image 38
TimS Avatar answered Nov 15 '22 17:11

TimS