views:

126

answers:

3

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?

+2  A: 

IMO, you should only call submit once.

Alternatively, I have a method to 'clear' the pending changes.

Have a look here.

leppie
Well If I understand right if it fails on the first insert(I am assuming it would go in order so Membership). It won't get added and it will never execute the User one. That script do I just call it or something?
chobo2
Oh do I use insertOnSubmit or InsertAllOnsubmit()? I am not sure what the diff is.
chobo2
One if for a single object, other for a list of objects.
leppie
+1  A: 

You only call submit after all the changes you would like bundled into a single transaction are applied to the LINQToSQL context.

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

Gregory
Cool Thats what I was thinking makes thing simple probably faster too.
chobo2
+3  A: 

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.

Jirapong