public void UpdateCustomer(Models.Customer customer) { if (customer is null) { //log it! throw new ArgumentNullException("Cannot update null customer"); } else { Customer cust = Mapper.Map(customer); //customer.Id is never null, no null check var existingCust = GetTById(cust.Id); if (existingCust != null) //if given customer is actually in db { //update local values Context.Entry(existingCust).CurrentValues.SetValues(cust); Context.SaveChanges(); //update db's values } else { //log it! throw new ArgumentOutOfRangeException("Customer with given id does not exist"); } } }
public void UpdateT(Location obj) { if (obj is null) { //log it! throw new ArgumentNullException("Cannot update null Location"); } else { //.Id is never null, no null check var existingLoc = GetTById(obj.Id); if (existingLoc != null) //if given Location is actually in db { //update local values //existingLoc.AddressId = obj.AddressId; //existingLoc.LocationName = obj.LocationName; //existingLoc.Customer = obj.Customer; //existingLoc.Inventory = obj.Inventory; //existingLoc.Orders = obj.Orders; Context.Entry(existingLoc).CurrentValues.SetValues(obj); //Context.Attach(existingLoc); //IEnumerable<EntityEntry> unchangedEntities = Context.ChangeTracker.Entries().Where(x => x.State == EntityState.Unchanged); //foreach (EntityEntry ee in unchangedEntities) //{ // ee.State = EntityState.Modified; //} //Context.Entry(existingLoc.Inventory).State = EntityState.Modified; //Context.Entry(existingLoc.Customer).State = EntityState.Modified; //Context.Entry(existingLoc.Orders).State = EntityState.Modified; Context.SaveChanges(); //update db's values } else { //log it! throw new ArgumentOutOfRangeException("Location with given id does not exist"); } } }