
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
None | size: 1.70 KB | hits: 8 | expires: Never
Adding and Editing using same connection in EF4
List<Campground> CampgroundEntities = new List<Campground>();
using (MiscEntities pd = new MiscEntities())
{
Campground kc = pd.Campground.FirstOrDefault(i => i.Name == name);
if (kc != null)
{
kc.StreetAddress = streetAddress;
kc.City = city;
kc.State = state;
kc.Zip = zip;
kc.URL = campgroundUrl;
kc.UpdatedDT = DateTime.Now;
CampgroundEntities.Add(kc);
}
else
{
kc = new Campground();
kc.Name = name;
kc.StreetAddress = streetAddress;
kc.City = city;
kc.State = state;
kc.Zip = zip;
kc.URL = campgroundUrl;
kc.AddedDateTime = DateTime.Now;
CampgroundEntities.Add(kc);
}
}
using (MiscEntities pd = new MiscEntities())
{
foreach (var item in CampgroundEntities)
{
pd.Campground.AddObject(item);
}
pd.SaveChanges();
}
using (MiscEntities pd = new MiscEntities())
{
Campground kc = pd.Campground.FirstOrDefault(i => i.Name == name);
if (kc == null)
{
kc = new Campground();
pd.Campground.Add(kc);
}
kc.StreetAddress = streetAddress;
kc.City = city;
kc.State = state;
kc.Zip = zip;
kc.URL = campgroundUrl;
kc.UpdatedDT = DateTime.Now;
CampgroundEntities.Add(kc);
}