1
MyDataContext context = new MyDataContext();

// do a lot of insert, deletes and updates

context.SubmitChanges(); 

Will all SQL genereated and executed by SubmitChanged() be covered by a transaction? How do I make sure it's covered by a transaction?

Updated:

The reason why I asking it that I having a weird bug where I suspect a transaction hasn't been used.

The procedure is about 500 inserts and a final update on one record. Sometimes the update (and perhaps a few of the insert...) isn't registered in the database.

(SQL Transactions isn't shown in my debug output?)

alexandrul
  • 12,856
  • 13
  • 72
  • 99
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148
  • http://stackoverflow.com/questions/542525/transactionscope-vs-transaction-in-linq2sql See the answer with the most votes. The accepted answer doesn't tell the whole story. – Ben Lesh Sep 17 '09 at 14:34

3 Answers3

4

it is, and the whole thing will roll back if it fails

How to: Submit Changes to the Database (LINQ to SQL)

Ahmed Khalaf
  • 1,220
  • 12
  • 28
3

If you want everything to happen within a transaction use a Transaction Scope

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
2

Explicitly creating a TransactionScope is only needed when you are invoking SubmitChanges multiple times and want all of the invokations to be included in one single transaction.

Konamiman
  • 49,681
  • 17
  • 108
  • 138