Tutorial :How can I tell datacontext I've updated a record via stored procedure


I have a stored procedure to update a record and after running it I use LinqToSql to delete the record. I know it is weird but I just want to test how smart the datacontext it is and understand how it works.

Since the datacontext caches the results so any change via it can be recorded but now I use a stored procedure to update something, it would not know. So when I try to delete it, an exception comes out "Row not found or changed".

How I can tell the datacontext what I have updated? If I can do so the problem will solve.


You can use the DataContext Refresh() method passing in a System.Data.Linq.RefreshMode enumeration; here's an example:

NorthWindDataContext context = new NorthwindDataContext();  context.Refresh(RefreshMode.KeepChanges);  

To Clarify:
The http://msdn.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx">RefreshMode enumeration has 3 values:

  1. KeepChanges
  2. KeepCurrentValues
  3. OverwriteCurrentValues

The version that used here (KeepChanges) forces the Refresh method to keep the modified value and update the other unmodified values by the value from the DB.


I guess I'm missing something, but if you know your stored proc just deletes the record, then why even bother with touching Linq2Sql after the fact? The DataContext isn't going to know about the deletion magically.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »