Reputation: 373
I have a controller and a repository. using EF 6
Controller file
int userID = _myrepository.addCustomer(model)
I get the userID
then using third party plugin to make a payment (ex:)
var success = makePaymentThroughPaymentProcessor(userID);
Now if the payment does not go through or an error happens I would like to rollback the changes so no customer is added not added to database.
I have tried using wrapping the repository method with
(var transaction = objMCOEntities.Database.BeginTransaction())
{
//code...
context.SaveChanges();
return userID;
I look over https://msdn.microsoft.com/en-us/data/dn456843.aspx
but I'm not sure how to reference the transaction after addCustomer repository methods has been called
I could take the payment first and then add the customer to the db after but if an error happens while adding the customer to db then customer is not created but the payment will be made and then I would have to refund customer
Upvotes: 3
Views: 4714
Reputation: 1969
If you're using Entity Framework 6, you could have a look at this page. Basically you'll have to choose between tranaction.Commit() and transaction.Rollback();
But I don't think this is really necessary in this scenario. If you have the right sequence in your code (first payment via third party tool and then save to db), you don't exactly need a transaction, but I don't know the specifics of this third party tool.
Upvotes: 1