public bool Create(ViewModels.CustomerGroupViewModel customerGroupViewModel) { bool ret = false; try { CustomerGroup CGroupdate = new CustomerGroup(); CGroupdate.CGroupID = customerGroupViewModel.GroupID; CGroupdate.CGroupFullName = customerGroupViewModel.FullName; CGroupdate.CGroupShortName = customerGroupViewModel.ShortName; CGroupdate.CGroupSW = customerGroupViewModel.GroupSW; db.CustomerGroup.Add(CGroupdate); db.SaveChanges(); ret = true; } catch { } return ret; }
public bool Update(ViewModels.CustomerGroupViewModel customerGroupViewModel) { bool ret = false; try { if (customerGroupViewModel.GroupID == customerGroupViewModel.OldGroupID) { CustomerGroup cGroup = db.CustomerGroup.Find(customerGroupViewModel.OldGroupID); cGroup.CGroupFullName = customerGroupViewModel.FullName; cGroup.CGroupShortName = customerGroupViewModel.ShortName; cGroup.CGroupSW = customerGroupViewModel.GroupSW; db.SaveChanges(); var qry = db.Customer.Where(x => x.GroupID == customerGroupViewModel.OldGroupID); foreach (Customer o in qry) { o.GroupSW = customerGroupViewModel.GroupSW; } db.SaveChanges(); ret = true; } else { CustomerGroup cGroup = new CustomerGroup(); cGroup.CGroupID = customerGroupViewModel.GroupID; cGroup.CGroupFullName = customerGroupViewModel.FullName; cGroup.CGroupShortName = customerGroupViewModel.ShortName; cGroup.CGroupSW = customerGroupViewModel.GroupSW; db.CustomerGroup.Add(cGroup); db.SaveChanges(); var qry = db.Customer.Where(x => x.GroupID == customerGroupViewModel.OldGroupID); foreach (Customer o in qry) { o.GroupID = customerGroupViewModel.GroupID; o.GroupSW = customerGroupViewModel.GroupSW; } db.SaveChanges(); CustomerGroup removeCGroup = db.CustomerGroup.Find(customerGroupViewModel.OldGroupID); db.CustomerGroup.Remove(removeCGroup); db.SaveChanges(); ret = true; } } catch (DbUpdateConcurrencyException ex) { // Update the values of the entity that failed to save from the store ex.Entries.Single().Reload(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } return ret; }