Skip to content

Instantly share code, notes, and snippets.

@jbogard
Last active August 29, 2015 14:18
Show Gist options
  • Save jbogard/e2926bedb1ab8d5786eb to your computer and use it in GitHub Desktop.
Save jbogard/e2926bedb1ab8d5786eb to your computer and use it in GitHub Desktop.
public void Txn(Action<MyContext> action)
{
var dbContext = new MyContext();
DbContextTransaction txn = null;
try
{
txn = dbContext.Database.BeginTransaction();
action(dbContext);
dbContext.SaveChanges();
txn.Commit();
}
catch (Exception)
{
txn?.Rollback();
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment