I've been seen many posts about using ef6 transactions but all the SaveChanges() are in the same block.
What I want is to use a transaction and call multiple functions inside a block, each one having SaveChanges() but belonging to the main transaction block.
I already tried code like the following:
using(var transaction = context.Database.BeginTransaction())
{
try
{
doSomething(); //Has SaveChanges() and also sub functions with also SaveChanges()
doSomethingElse(); //Same as before
}
catch (Exception exp)
{
transaction.Rollback();
}
transaction.Commit();
}
What happens is that transaction.Rollback() does nothing at all.
I assume that the inner functions have their own transaction scope and don't care about this one. So how can I put this to work?
I did a quick check in LinqPad:
which results in:
The rollback works.
Maybe your doSomthing-Methods did not throw an exception so the rollback never happened. Could you please check?