public bool Update(Customer item) { if (item == null) throw new ArgumentNullException("Customer is null"); if (Get(item.Id) == null) return false; using (Context con = new Context()) { if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null) throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress); if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address)) throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress); } using (Context con = new Context()) { var temp = con.Customer.Find(item.Id); con.Customer.Attach(temp); con.Address.Attach(item.Address); temp.Address = item.Address; con.Entry(temp).CurrentValues.SetValues(item); con.Entry(temp).State = EntityState.Modified; con.SaveChanges(); return true; } }
public bool Update(Ingredient item) { if (item == null) throw new ArgumentNullException("Ingredient is null"); if (Get(item.Id) == null) return false; using (Context con = new Context()) { con.Ingredient.Attach(item); con.Entry(item).State = System.Data.Entity.EntityState.Modified; con.SaveChanges(); return true; } }
public bool Update(Party item) { if (item == null) throw new ArgumentNullException("Party is null"); if (Get(item.Id) == null) return false; using (Context con = new Context()) { foreach (var ing in item.Menus) { if (con.Menu.FirstOrDefault(x => x.Id == ing.Id) == null) throw new ArgumentException("You need to add the menu to the database first: " + ing.Name); if (!con.Menu.FirstOrDefault(x => x.Id == ing.Id).Equals(ing)) throw new ArgumentException("You need to update the menu first: " + ing.Name); } if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null) throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress); if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address)) throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress); if (con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id) == null) throw new ArgumentException("You need to add the customer to the database first: " + item.Customer.FirstName); if (!con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id).Equals(item.Customer)) throw new ArgumentException("You need to update the customer first: " + item.Customer.FirstName); } using (Context con = new Context()) { //Changes party properties Party temp = con.Party.Find(item.Id); con.Party.Attach(temp); con.Entry(temp).CurrentValues.SetValues(item); temp.Menus = con.Party.Include("Menus").FirstOrDefault(x => x.Id == item.Id).Menus.ToList(); //New address if (!temp.Address.Equals(item.Address)) { con.Address.Attach(item.Address); temp.Address = item.Address; } //New Customer if (!temp.Customer.Equals(item.Customer)) { con.Customer.Attach(item.Customer); temp.Customer = item.Customer; } //Adds new menus foreach (var ing in item.Menus.ToList()) { if (!temp.Menus.Any(x => x.Id == ing.Id)) { con.Menu.Attach(ing); temp.Menus.Add(ing); } } //Deletes removed menus foreach (var ing in temp.Menus.ToList()) { if (!item.Menus.Any(x => x.Id == ing.Id)) { con.Menu.Attach(ing); temp.Menus.Remove(ing); } } //Saves changes con.Entry(temp).State = EntityState.Modified; con.SaveChanges(); return true; } }
public bool Update(Menu item) { if (item == null) throw new ArgumentNullException("Menu is null"); if (Get(item.Id) == null) return false; using (Context con = new Context()) { foreach (var ing in item.Dishes) { if (con.Dish.FirstOrDefault(x => x.Id == ing.Id) == null) throw new ArgumentException("You need to add the dish to the database first: " + ing.Name); if (!con.Dish.FirstOrDefault(x => x.Id == ing.Id).Equals(ing)) throw new ArgumentException("You need to update the dish first: " + ing.Name); } } using (Context con = new Context()) { var temp = con.Menu.Find(item.Id); con.Menu.Attach(temp); con.Entry(temp).CurrentValues.SetValues(item); foreach (var ing in item.Dishes.ToList()) { if (!temp.Dishes.Any(x => x.Id == ing.Id)) { con.Dish.Attach(ing); temp.Dishes.Add(ing); } } foreach (var ing in temp.Dishes.ToList()) { if (!item.Dishes.Any(x => x.Id == ing.Id)) { con.Dish.Attach(ing); temp.Dishes.Remove(ing); } } con.Entry(temp).State = EntityState.Modified; con.SaveChanges(); return true; } }