public bool Delete(Guid parentUid, int profileId, ref string statusMessage) { bool isValid = false; if (profileId == 0) { statusMessage = "Error - missing an identifier for the Assessment to remove"; return(false); } //need to get Entity.Id Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { statusMessage = "Error - the parent entity was not found."; return(false); } using (var context = new EntityContext()) { DBEntity efEntity = context.Entity_CommonCondition .SingleOrDefault(s => s.EntityId == parent.Id && s.ConditionManifestId == profileId); if (efEntity != null && efEntity.Id > 0) { context.Entity_CommonCondition.Remove(efEntity); int count = context.SaveChanges(); if (count > 0) { isValid = true; } } else { statusMessage = "Warning - the record was not found - probably because the target had been previously deleted"; isValid = true; } } return(isValid); }
/// <summary> /// Add an Entity_CommonCondition /// </summary> /// <param name="parentUid"></param> /// <param name="profileId"></param> /// <param name="messages"></param> /// <returns></returns> private int Save(Entity parent, int conditionManifestId, ref SaveStatus status) { int id = 0; if (conditionManifestId == 0) { status.AddError(string.Format("A valid ConditionManifest identifier was not provided to the {0}.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); } //need to check the whole chain if (parent.EntityTypeId == CodesManager.ENTITY_TYPE_CONDITION_MANIFEST && parent.EntityBaseId == conditionManifestId) { status.AddError("Error - you cannot add a condition manifest as a common condition to itself."); return(0); } using (var context = new EntityContext()) { DBEntity efEntity = new DBEntity(); try { //first check for duplicates efEntity = context.Entity_CommonCondition .SingleOrDefault(s => s.EntityId == parent.Id && s.ConditionManifestId == conditionManifestId); if (efEntity != null && efEntity.Id > 0) { status.AddError(string.Format("Error - this ConditionManifest has already been added to this profile.", thisClassName)); return(0); } efEntity = new DBEntity(); efEntity.EntityId = parent.Id; efEntity.ConditionManifestId = conditionManifestId; efEntity.Created = System.DateTime.Now; context.Entity_CommonCondition.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { id = efEntity.Id; return(efEntity.Id); } else { //?no info on error status.AddError("Error - the add was not successful."); string message = thisClassName + string.Format(".Add Failed", "Attempted to add a ConditionManifest for a profile. The process appeared to not work, but there was no exception, so we have no message, or no clue. Parent Profile: {0}, Type: {1}, conditionManifestId: {2}", parent.EntityUid, parent.EntityType, conditionManifestId); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } 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); }