Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the LINQ RefreshMode work?

In case of conflict, I need to overwrite the values in the database with my changes . I found the following article on MSDN that explains how to resolve conflicts using the RefreshMode:

http://msdn.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx

I decided KeepCurrentValues makes sense for my requirement and I also found this page with an example for this mode:

http://msdn.microsoft.com/en-us/library/bb399421.aspx

However when I implemented this the values from the database always overwrote my changes. I tried changing the RefreshMode to OverwriteCurrentValues and KeepChanges and each time the values from the database were saved. My approach was to manually change values in the database while in debug mode in VisualStudio. The code in VS is:

[MyDataContext] db = new [MyDataContext]();
try
{
    [MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
    old.IntField = 55;

    // This is where I change the IntField value manually in the database in
    // debug mode to generate the conflict.
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
    db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
}

I know that the conflict appears but every time, no matter how I change the RefreshMode, the value 55 is never saved and the changes that I made manually in the database are kept. Is there some trick to achieve the desired result? I have tried generating the conflict from inside the code at first and that didn't work as expected either. Maybe I didn't understand how the RefreshMode should work. Any ideas are welcome.

like image 205
Ioana Marcu Avatar asked Mar 30 '11 08:03

Ioana Marcu


2 Answers

You just need to call SubmitChanges again...

catch (ChangeConflictException)
{            
    db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);  
    db.SubmitChanges(); 
} 

The conflict resolution method determines the state of the DataContext and not the underlying database.

KeepCurrentValues means keep all values as they are currently in the DataContext, which means the next call to SubmitChanges will save any changes made by the user, but it will also overwrite any changes made by other users after the data was loaded by the current user.

KeepChanges means keep only the values that have been changed since being loaded into the DataContext, which means the next call to SubmitChanges will save any changes made by the user and will preserve any changes made by other users. And, if another user changed the same value as the current user, the current user's change will overwrite it.

OverwriteCurrentValues means update the DataContext with the current database values, which means that all changes made by the current user will be discarded.

like image 172
CFinck Avatar answered Oct 29 '22 22:10

CFinck


Before changing data, you refresh your object by overwriting from Db :

[MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
Db.Refresh(RefreshMode.OverwriteCurrentValues, old);
old.IntField = 55;
db.SubmitChanges(ConflictMode.ContinueOnConflict);
like image 22
Nath Avatar answered Oct 29 '22 21:10

Nath