2012-06-26

Transactions with a datacontext

1. Transaction

dataContext.Connection.Open();
using (datacontext.Transaction = dataContext.Connection.BeginTransaction())
{
    dataContext.SubmitChanges();

    if (allOK)
    {
        datacontext.Transaction.Commit();
    }
    else
    {
        datacontext.Transaction.RollBack();
    }
}


2.TransactionScope

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        //Do some stuff

        //Submit changes, use ConflictMode to specify what to do
        context.SubmitChanges(ConflictMode.ContinueOnConflict);

        scope.Complete();
    }
}
catch (ChangeConflictException cce)
{
        //Exception, as the scope was not completed it will rollback
}



adsense