Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I migrate a temp table using LINQ to SQL?
  2. // migrate temp profile(s)...
  3.             var tempProfilesToMigrate = from ct in db.contact_temps
  4.                                          where ct.SessionKey == contact.Profile.SessionId
  5.                                          select new contact();
  6.  
  7.  
  8.  
  9.             db.contacts.InsertAllOnSubmit(tempProfilesToMigrate);
  10.             db.SubmitChanges();
  11.  
  12.             //...clear temp table records
  13.             var tempProfilesToDelete = from ct in db.contact_temps
  14.                                         where ct.SessionKey == contact.Profile.SessionId
  15.                                         select ct;
  16.  
  17.             db.contact_temps.DeleteAllOnSubmit(tempProfilesToDelete);
  18.             db.SubmitChanges();
  19.        
  20. // migrate temp profile(s)...
  21. var tempProfiles = from ct in db.contact_temps
  22.                              where ct.SessionKey == contact.Profile.SessionId
  23.                              select ct;
  24.  
  25. foreach (var c in tempProfiles)
  26. {
  27.     Contact newC = new Contact();
  28.     newC.Name = c.Name;
  29.     // copy other values
  30.  
  31.     db.contacts.InsertOnSubmit(newC);
  32. }
  33.  
  34. // WAIT! do it at once in a single TX => avoid db.SubmitChanges() here.
  35.  
  36.  db.contact_temps.DeleteAllOnSubmit(tempProfiles);
  37.  
  38.  // Both sets of changes in one Tx.
  39.  db.SubmitChanges();