I assumed that when using a DataContext I get an automatic rollback:
UPDATE I actually called SubmitChangestwice, but the question still applies.
public void UpdateUser(User user)
{
using (var context = new UserDataContext())
{
context.SubmitChanges();
context.SubmitChanges();
}
}
When something goes wrong, there is no rollback.
Instead, to provide a rollback, I implemented the following:
public void UpdateUser(User user)
{
var context = new UserDataContext();
try
{
context.Connection.Open();
context.Transaction = context.Connection.BeginTransaction();
context.SubmitChanges();
context.Transaction.Commit();
}
catch (Exception e)
{
context.Transaction.Rollback();
throw;
}
finally
{
context.Dispose();
}
}
which is much more than i want. Is there a better way to tell the DataContext that you want automatic rollback?
source
share