EF multi-database transactions

using (TransactionScope scope = new TransactionScope())
{
    //Do something with context1
    //Do something with context2

    //Save Changes but don't discard yet
    context1.SaveChanges(false);

    //Save Changes but don't discard yet
    context2.SaveChanges(false);

    //if we get here things are looking good.
    scope.Complete();
    context1.AcceptAllChanges();
    context2.AcceptAllChanges();

}

 We send a SaveChanges (false) necessary first database operation commands to the database, which is attention context1 and context2 not really changed, if the transaction is terminated, automatic rollback, both changes are not really committed to the database, it is It can be successfully rolled back.

Guess you like

Origin www.cnblogs.com/netcs/p/12427398.html