title: How to create a LINQ to SQL Transaction?
body: <p>I have a piece of code that involves multiple inserts but need to execute submitchanges method before I finish inserting in other tables so that I can aquire an Id. I have been searching through the internet and couldnt find how to create a transaction in linq to sql. I have put comments in the code where I want the transaction to take place.</p>
  
  <pre><code>    var created = false;
      try
      {
          var newCharacter = new Character();
          newCharacter.characterName = chracterName;
          newCharacter.characterLevel = 1;
          newCharacter.characterExperience = 0;
          newCharacter.userUsername = userUsername;
          newCharacter.characterClassID = ccslst[0].characterClassID;
          //Open transaction
  
  
              ydc.Characters.InsertOnSubmit(newCharacter);
              ydc.SubmitChanges();
  
              foreach (var ccs in ccslst)
              {
                  var cs = new CharacterStat();
                  cs.statId = ccs.statID;                        
                  cs.statValue = ccs.statValue;
                  cs.characterID = newCharacter.characterID;
                  ydc.CharacterStats.InsertOnSubmit(cs);
              }                    
  
  
              var ccblst = ydc.ClassBodies.Where(cb =&gt; cb.characterClassID == newCharacter.characterClassID);
              foreach (var ccb in ccblst)
              {
                  var charBody = new CharacterBody();
                  charBody.bodyId = ccb.bodyId;
                  charBody.bodyPartId = ccb.bodyPartId;
                  charBody.characterID = newCharacter.characterID;
                  ydc.CharacterBodies.InsertOnSubmit(charBody);
              }
              ydc.SubmitChanges();      
              created = true;
          //Commit transaction
          }
          catch (Exception ex)
          {
              created = false;
              //transaction Rollback;                    
          }
          return created;
  </code></pre>
  
  <p>EDIT: Forgot to mention that ydc is my datacontext</p>