I am trying to figure out what is the correct way to start a transaction and prepare for rollback in case of error...
I notice there is DbContextScopeFactory.CreateWithTransaction(IsolationLevel) in the code but couldn't figure it out how to actually use it.
Is this the proper way to do it?
using (var dbContextScope = DbContextScopeFactory.CreateWithTransaction(IsolationLevel.ReadCommitted))
{
var dbContext = dbContextScope.DbContexts.Get<MyDbContext>();
using (var trans = dbContext.Database.BeginTransaction())
{
try
{
... do some work here.. modify several tables etc..
dbContext.SaveChanges();
trans.Commit();
}
catch (Exception)
{
trans.Rollback(); //rolback everything
throw;
}
}
}
I am trying to figure out what is the correct way to start a transaction and prepare for rollback in case of error...
I notice there is
DbContextScopeFactory.CreateWithTransaction(IsolationLevel)
in the code but couldn't figure it out how to actually use it.Is this the proper way to do it?