How to detach a LINQ-to-SQL data object from the DataContext's tracking mechanism?

smartcaveman picture smartcaveman · Mar 9, 2011 · Viewed 10.9k times · Source

After asking this question, where I was informed of how the Table<T>.Attach() method works, I have another question.

How do you detach a LINQ-to-SQL data object from the DataContext's state tracking mechanism? Basically, I want to pull a record and change the data on the record. But, when I call SubmitChanges() on the same DataContext instance, I do not want the record to be updated unless I have explicitly called Attach(). How is this accomplished?

Answer

shaunmartin picture shaunmartin · Mar 9, 2011

I strongly recommend that if you're going to use LINQ to SQL, you should change your design to accommodate LINQ to SQL's behavior of submitting changes on all attached modified entities. In my experience, attempting to work around this feature will only lead to pain.