If LINQ to SQL determines that a call is in the scope of a
transaction, a new transaction is not created. As with an explicit transaction, the user is responsible for
the creation, committing, and disposing of the transaction.
The following examples illustrate a couple of these transaction modes. In the first example, a specific
transaction scope is created and several operation calls are executed within this scope. Because
a transaction scope is used within a using statement, a specific commit or rollback is not necessary.
In this example, a TransactionScope is created and several insert operations are performed and the
SubmitChanges method is called. The TransactionScope class, part of the System.Transactions namespace,
marks a block of code as transactional by implicitly enlisting connections within its transaction.
As discussed earlier, an implicit transaction must be manually committed or rolled back by the user/-
application.
The following example explicitly creates a transaction using the TransactionScope class to mark a block
of code as included in a transaction:
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
try
{
using (TransactionScope ts = new TransactionScope())
{
Contact con = new Contact();
con.
Pages:
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431