How to use transactions with a datacontext

user107534 picture user107534 · May 15, 2009 · Viewed 40.7k times · Source

Can I use transactions with a datacontext, so that I can rollback the state of the context after an error? And if so, how does that work?

Answer

leppie picture leppie · May 15, 2009

I use them in testing all the time :)

try
{
  dc.Connection.Open();
  dc.Transaction = dc.Connection.BeginTransaction();

  dc.SubmitChanges();
}
finally
{
  dc.Transaction.Rollback();
}

UPDATE

This will ALWAYS rollback after the fact. I use this in testing.