tags:

views:

41

answers:

3

Is it safe to call SubmitChanges() when you are not sure whether the data has changed? What does it actually do?

+1  A: 

Yeah, its fine, you just won't have anything happening but it won't hurt anything. It basically converts your changes into appropriate SQL and executes the commands against the database.

kekekela
+1  A: 

Yes this is safe. Essentially, LINQ->SQL will examine the set of tracked objects for changes. If any changes were detected it will begin a transaction and apply the changes. If there were no changes, it will do nothing.

See this MSDN article for the order of operations for SubmitChanges.

Remember, the method is called SubmitChanges - meaning that it will only submit changes that have been made to the entity.

Sam
+1  A: 

It's safe, but you should do some testing yourself. I always set the .Log property on my DataContext to log to the debug window. You may discover that you're doing updates you didn't intend to because the data has changed in a way you didn't expect. It's always good to watch what SQL is being generated and how many database calls are happening.

mattmc3
+1 for introducing me to `.Log`
Graphain