public void Add(PhotoDTO pdto) { foreach (Photo p in pdto.Photos) { //First, turn off the current IsPrimary for this user if this pic is priamary. Code referenced from following url: //http://www.dotnetlearners.com/blogs/view/142/update-multiple-records-in-entity-framework-in-a-single-line-by-using-linq-query.aspx if (p.IsPrimary == true) { base.Photos.Where(x => x.ID == p.ID && x.IsPrimary == true).ToList().ForEach(x => { x.IsPrimary = false; }); } //now insert base.Photos.Add(p); } SaveChanges(); }
public void Update(PhotoDTO pdto) { foreach (Photo p in pdto.Photos) { //First, turn off the current IsPrimary for this user. Code referenced from following url: //http://www.dotnetlearners.com/blogs/view/142/update-multiple-records-in-entity-framework-in-a-single-line-by-using-linq-query.aspx if (p.IsPrimary == true) { base.Photos.Where(x => x.ID == p.ID && x.IsPrimary == true).ToList().ForEach(x => { x.IsPrimary = false; }); } //Now, the update with new IsPrimary base.Photos.Attach(p); base.Entry(p).Property(a => a.IsPrimary).IsModified = true; } SaveChanges(); }