Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollback with linq to sql?

I have this

Public void CreateUser(params here)
{
   CreateMembership(params here);
   Users newUser = new Users();
   newUser.UserId = 123456
   Context.Users.insertOnSubmit();
   Context.Submit();
}

public void CreateMembership(...)
{
    Membership membership = new Membership();
     membership.Something = "something";
     Context.Membership.insertOnSumbit();
     Context.Submit();
}

So what happen if the Users table submit fails how can I rol this back to delete the Membership stuff? Or can I setup my thing differently like only remove the Context.Submit() line out of the Membership method?

Then only one Submit gets called? Or do I have to do something else?

like image 350
chobo2 Avatar asked Jul 09 '26 11:07

chobo2


1 Answers

Using of TransactionScope should be my suggestion.

using (TransactionScope ts = new TransactionScope())
{
    myContext.SubmitChanges();
    ts.Complete();
}

It will rollback, if any exceptions throw from SubmitChanges() method.

like image 166
Jirapong Avatar answered Jul 12 '26 00:07

Jirapong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!