public bool Delete(Guid parentUid, int recordId, ref string statusMessage) { bool isValid = false; if (recordId == 0) { statusMessage = "Error - missing an identifier for the Entity_HasPathway 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_HasPathway .FirstOrDefault(s => s.EntityId == parent.Id && s.PathwayId == recordId); if (efEntity != null && efEntity.Id > 0) { context.Entity_HasPathway.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> /// Update: check for existing. Any existing pathways that were not included in the input will be removed. /// </summary> /// <param name="parentUid"></param> /// <param name="pathwayRelationshipTypeId"></param> /// <param name="inputIds"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Replace(Guid parentUid, int pathwayRelationshipTypeId, List <int> inputIds, ref SaveStatus status) { if (inputIds == null || !inputIds.Any()) { return(true); } status.HasSectionErrors = false; Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(false); } int count = 0; DBEntity efEntity = new DBEntity(); try { using (var context = new EntityContext()) { var existing = context.Entity_HasPathway.Where(s => s.EntityId == parent.Id && s.PathwayRelationshipTypeId == pathwayRelationshipTypeId).ToList(); //var inputIds = profiles.Select( x => x.Id ).ToList(); var existingIds = existing.Select(x => x.PathwayId).ToList(); //delete records which are not selected var notExisting = existing.Where(x => !inputIds.Contains(x.PathwayId)).ToList(); foreach (var item in notExisting) { context.Entity_HasPathway.Remove(item); context.SaveChanges(); } //only get profiles where not existing var newProfiles = inputIds.Where(x => !existingIds.Contains(x)).ToList(); if (existing != null && existing.Count() > 0 && inputIds.Count() > 0) { LoggingHelper.DoTrace(6, string.Format(thisClassName + ".Replace. Existing: {0}, input: {1}, Not existing(to delete): {2}, newProfiles: {3}", existing.Count(), inputIds.Count(), notExisting.Count(), newProfiles.Count())); if (existing.Count() != inputIds.Count()) { } } foreach (var pathwayId in newProfiles) { //if there are no existing, optimize by not doing check. What about duplicates? efEntity = new DBEntity { EntityId = parent.Id, PathwayRelationshipTypeId = pathwayRelationshipTypeId, PathwayId = pathwayId, Created = DateTime.Now, }; context.Entity_HasPathway.Add(efEntity); count = context.SaveChanges(); } //foreach } } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError(message); LoggingHelper.LogError(ex, "Entity_FrameworkItemManager.Replace()"); } return(status.WasSectionValid); } //
/// <summary> /// Add an Entity HasPathway /// </summary> /// <param name="parentUid"></param> /// <param name="pathwayId"></param> /// <param name="pathwayRelationshipTypeId">May not be relevent, default to 1</param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public int Add(Guid parentUid, int pathwayId, int pathwayRelationshipTypeId, ref SaveStatus status) { int id = 0; status.HasSectionErrors = false; if (pathwayId == 0) { status.AddError(string.Format("A valid Entity_HasPathway identifier was not provided to the {0}.Add method.", thisClassName)); } if (pathwayRelationshipTypeId == 0) { pathwayRelationshipTypeId = 1; //messages.Add( string.Format( "A valid Entity_HasPathway PathwayRelationshipTypeId was not provided to the {0}.Add method.", thisClassName ) ); } if (status.HasSectionErrors) { 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 { //first check for duplicates efEntity = context.Entity_HasPathway .FirstOrDefault(s => s.EntityId == parent.Id && s.PathwayId == pathwayId && s.PathwayRelationshipTypeId == pathwayRelationshipTypeId); if (efEntity != null && efEntity.Id > 0) { if (ReturningErrorOnDuplicate) { status.AddError(string.Format("Error - this Entity_HasPathway has already been added to this profile.", thisClassName)); } return(efEntity.Id); } efEntity = new DBEntity(); efEntity.EntityId = parent.Id; efEntity.PathwayId = pathwayId; efEntity.PathwayRelationshipTypeId = pathwayRelationshipTypeId; efEntity.Created = System.DateTime.Now; context.Entity_HasPathway.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { //messages.Add( "Successful" ); 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 a Entity_HasPathway 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}, learningOppId: {2}", parentUid, parent.EntityType, pathwayId); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity_Pathway"); 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); }