This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Thursday, September 24, 2009

Linq to SQL Change Conflicts

I had been getting two similar but different errors when trying to persist records to an MS SQL 2008 database with Linq to SQL.  The exception was of type System.Data.Linq.ChangeConflictException and the details were either “Row not found or changed” or “1 of x updates failed” when I dug a little deeper.

The instance of your DataContext will contain a collection of ChangeConflictException objects.  You can foreach through the collection and resolve the conflict.  Here’s an example of how:

image 



The next time you call SubmitChanges the record in question will be saved.  Calling the Resolve method requires that you give the DataContext a resolution strategy.  In my scenario, I choose to merge the data into the saved record in the event of a conflict.  I believe what is happening is that I’m getting a collision because I’m creating multiple child records in different threads (and contexts) and trying to save, but I have to suss this out a little better.



I am running about 20 threads and polling 3500 devices.  Sometimes devices enter a state where a scheduled poll fails and gets into the queue again (for a refresh) around the same time as the next scheduled poll.



Obviously I’ll be working to refine that so if a fail occurs I can skip (or reschedule) the next poll and not have to worry about the results coming back in the same frame.

No comments:

Post a Comment