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

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 1.70 KB  |  hits: 8  |  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. Adding and Editing using same connection in EF4
  2. List<Campground> CampgroundEntities = new List<Campground>();
  3.  
  4.  using (MiscEntities pd = new MiscEntities())
  5.             {
  6.                 Campground kc = pd.Campground.FirstOrDefault(i => i.Name == name);
  7.  
  8.                 if (kc != null)
  9.                 {
  10.                     kc.StreetAddress = streetAddress;
  11.                     kc.City = city;
  12.                     kc.State = state;
  13.                     kc.Zip = zip;
  14.                     kc.URL = campgroundUrl;
  15.                     kc.UpdatedDT = DateTime.Now;
  16.  
  17.                     CampgroundEntities.Add(kc);
  18.                 }
  19.                 else
  20.                 {
  21.                     kc = new Campground();
  22.                     kc.Name = name;
  23.                     kc.StreetAddress = streetAddress;
  24.                     kc.City = city;
  25.                     kc.State = state;
  26.                     kc.Zip = zip;
  27.                     kc.URL = campgroundUrl;
  28.                     kc.AddedDateTime = DateTime.Now;
  29.  
  30.                     CampgroundEntities.Add(kc);
  31.                 }
  32.             }
  33.        
  34. using (MiscEntities pd = new MiscEntities())
  35.         {
  36.             foreach (var item in CampgroundEntities)
  37.             {
  38.                 pd.Campground.AddObject(item);
  39.             }
  40.             pd.SaveChanges();
  41.         }
  42.        
  43. using (MiscEntities pd = new MiscEntities())
  44. {
  45.     Campground kc = pd.Campground.FirstOrDefault(i => i.Name == name);
  46.  
  47.     if (kc == null)
  48.     {
  49.         kc = new Campground();
  50.         pd.Campground.Add(kc);          
  51.     }
  52.  
  53.     kc.StreetAddress = streetAddress;
  54.     kc.City = city;
  55.     kc.State = state;
  56.     kc.Zip = zip;
  57.     kc.URL = campgroundUrl;
  58.     kc.UpdatedDT = DateTime.Now;
  59.  
  60.     CampgroundEntities.Add(kc);
  61. }