public bool Delete(int id, ref List <string> messages) { bool isValid = true; if (id < 1) { messages.Add(thisClassName + ".Delete() Error - a valid Entity_EmploymentOutcomeProfile id must be provided."); return(false); } using (var context = new EntityContext()) { try { context.Configuration.LazyLoadingEnabled = false; DBEntity efEntity = context.Entity_EmploymentOutcomeProfile .FirstOrDefault(s => s.Id == id); if (efEntity != null && efEntity.Id > 0) { // context.Entity_EmploymentOutcomeProfile.Remove(efEntity); int count = context.SaveChanges(); if (count > 0) { isValid = true; } } else { messages.Add(thisClassName + ".Delete() Warning No action taken, as the Entity_EmploymentOutcomeProfile was not found."); } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Delete()"); isValid = false; var statusMessage = FormatExceptions(ex); if (statusMessage.ToLower().IndexOf("the delete statement conflicted with the reference constraint") > -1) { statusMessage = "Error: this Entity_EmploymentOutcomeProfile cannot be deleted as it is being referenced by other items, such as roles or credentials. These associations must be removed before this Entity_EmploymentOutcomeProfile can be deleted."; } messages.Add(statusMessage); } } return(isValid); }
/// <summary> /// Add an Entity_EmploymentOutcomeProfile /// </summary> /// <param name="parentUid"></param> /// <param name="EmploymentOutcomeProfileId"></param> /// <param name="messages"></param> /// <returns></returns> public int Add(Guid parentUid, int employmentOutcomeProfileId, ref SaveStatus status) { int id = 0; int count = 0; if (employmentOutcomeProfileId == 0) { status.AddError(string.Format("A valid EmploymentOutcomeProfile identifier was not provided to the {0}.EntityEarnings_Add method.", thisClassName)); return(0); } Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(0); } using (var context = new EntityContext()) { DBEntity efEntity = new DBEntity(); try { efEntity = context.Entity_EmploymentOutcomeProfile .FirstOrDefault(s => s.EntityId == parent.Id && s.EmploymentOutcomeProfileId == employmentOutcomeProfileId); if (efEntity != null && efEntity.Id > 0) { return(0); } efEntity = new DBEntity { EntityId = parent.Id, EmploymentOutcomeProfileId = employmentOutcomeProfileId, Created = System.DateTime.Now }; context.Entity_EmploymentOutcomeProfile.Add(efEntity); // submit the change to database count = context.SaveChanges(); if (count > 0) { if (efEntity.Id == 0) { List <string> messages = new List <string>(); Delete(efEntity.Id, ref messages); context.Entity_EmploymentOutcomeProfile.Add(efEntity); count = context.SaveChanges(); id = efEntity.Id; } else { id = efEntity.Id; } return(efEntity.Id); } else { //?no info on error status.AddError(thisClassName + "Error - the add was not successful."); string message = thisClassName + string.Format(".Add Failed", "Attempted to add an Entity_EmploymentOutcomeProfile. The process appeared to not work, but there was no exception, so we have no message, or no clue. Parent Profile: {0}, Type: {1}, EarningsId: {2}", parentUid, parent.EntityType, employmentOutcomeProfileId); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity_Earnings"); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } } return(id); }