Comma
Comma

Reputation: 1607

DataContext SubmitChanges in LINQ

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

Upvotes: 1

Views: 1490

Answers (3)

mattmc3
mattmc3

Reputation: 18315

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.

Upvotes: 3

Sam
Sam

Reputation: 770

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.

Upvotes: 1

heisenberg
heisenberg

Reputation: 9759

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.

Upvotes: 1

Related Questions