Entity Framework 7 Rollback method

asp.net-core c# entity-framework-core

Question

How to roll back an transaction in Entity Framework 7?

In the old version, we have

            try
            {
                dataContext.ChangeTracker.DetectChanges();
                dataContext.SaveChanges();
            }
            catch (Exception e)
            {
                dataContext.Database.CurrentTransaction.Rollback();
                Debug.WriteLine(e.Message);
            }

Now I use

"EntityFramework.Commands": "7.0.0-rc1-final"

It doesn't have the method.

1
3
5/16/2016 7:40:01 PM

Accepted Answer

The recommended transaction pattern in 7.0.0-rc1-final is the same as in EF6:

using (var transaction = context.Database.BeginTransaction())
{
    try
    {
        /*do something*/
        context.SaveChanges();

        transaction.Commit();
    }
    catch (Exception ex)
    {
        transaction.Rollback();
    }
}

Also EF7 provides async API (EF6 not):

using (var transaction = await context.Database.BeginTransactionAsync())
6
5/16/2016 10:16:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow