Rollback from linq to sql?

I have it

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 happens if the Users table is submitted with an error, how can I return it to remove the membership? Or can I customize my thing differently as soon as I remove the Context.Submit () line from the Membership method?

Then only one Submit is called? Or do I need to do something else?

+2


source to share


3 answers


IMO you only have to call once.

Alternatively, I have a way to "clean up" the pending changes.



Take a look here .

+5


source


Using TransactionScope should be my suggestion.

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

      



The rollback will be canceled if any exceptions are thrown from the SubmitChanges () method.

+7


source


You only call submit after all the changes you would like to add in one transaction have been applied to the LINQToSQL context.

Essentially remove Context.Submit (); from the CreateMembership function and you will get the result you are looking for.

+2


source







All Articles