Is it safe to call SubmitChanges() when you are not sure whether the data has changed? What does it actually do?
问题:
回答1:
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.
回答2:
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.
回答3:
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.