public bool Create(ViewModels.EventClassViewModel eventClassViewModel) { bool ret = false; try { EventClass eventClass = new EventClass(); eventClass.Class = eventClassViewModel.Class; eventClass.Name = eventClassViewModel.Name; db.EventClass.Add(eventClass); db.SaveChanges(); ret = true; } catch { } return ret; }
public bool Update(ViewModels.EventClassViewModel eventClassViewModel) { bool ret = false; try { if (eventClassViewModel.Class == eventClassViewModel.OldClass) { EventClass e = db.EventClass.Find(eventClassViewModel.OldClass); e.Name = eventClassViewModel.Name; db.SaveChanges(); ret = true; } else { EventClass e = new EventClass(); e.Class = eventClassViewModel.Class; e.Name = eventClassViewModel.Name; db.EventClass.Add(e); db.SaveChanges(); var qry = db.WBSEventC.Where(x => x.EventClass == eventClassViewModel.OldClass); foreach (WBSEventC o in qry) { o.EventClass = eventClassViewModel.Class; } db.SaveChanges(); EventClass e2 = db.EventClass.Find(eventClassViewModel.OldClass); db.EventClass.Remove(e2); 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; }