Skip to content

Instantly share code, notes, and snippets.

@njmube
Forked from jbogard/Txn.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save njmube/3af7d18ebff96041136e to your computer and use it in GitHub Desktop.
Save njmube/3af7d18ebff96041136e 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