Rollback EF6 Transaction

We are extending the context object to have synchronization functionality with another system. We are currently doing this in savechanges. However, we realized that there is a case where the context would be used in a transaction state, using begintransaction with multiple calls to persist the changes. We don't want to sync the data until the full process is complete. We thought about doing this to get rid of the context.

The only problem we have is knowing if the transaction is successful. If that succeeds, then great sync. If it rolled back, then we certainly don't want to sync. How do we know the state of a transaction when the context is deleted?

+3


source to share


3 answers


Ask for your models that require synchronization to implement the iSyncable interface, for example promises, which you will synchronize once the model is successfully saved. If you have linked tables via navigation properties, save everything and put them in navigation properties before synchronizing them so that your sync function has all the objects it needs.



By the way, if you need to do any data transformations, this will be where you do it.

+1


source


You need to enlist your transaction in the transaction manager to receive transaction related notifications.



MSDN IEnlistmentTransaction

+1


source


Don't do this in the delete method. Disposal is for cleaning purposes. Use the following template:

using (var tran = new TransactionScope())
using (var db = new MyContext())
{
 db.Connection.Open();
 DoWork(db);
 tran.Complete();
}

      

That's all you need to do.

0


source







All Articles