public bool DeleteAll(int costProfileId, ref SaveStatus status) { bool isValid = false; int expectedDeleteCount = 0; using (var context = new EntityContext()) { try { var results = context.Entity_CostProfileItem.Where(s => s.CostProfileId == costProfileId) .ToList(); if (results == null || results.Count == 0) { return(true); } expectedDeleteCount = results.Count; foreach (var item in results) { context.Entity_CostProfileItem.Remove(item); int count = context.SaveChanges(); if (count > 0) { isValid = true; } else { //if doing a delete on spec, may not have been any properties } } } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException dbcex) { if (dbcex.Message.IndexOf("an unexpected number of rows (0)") > 0) { //don't know why this happens, quashing for now. LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. costProfileId: {0}, expectedDeletes: {1}. DbUpdateConcurrencyException: {2}", costProfileId, expectedDeleteCount, dbcex.Message)); isValid = true; } else { var msg = BaseFactory.FormatExceptions(dbcex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. costProfileId: {0}, DbUpdateConcurrencyException: {1}", costProfileId, msg)); } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. costProfileId: {0}, Exception: {1}", costProfileId, msg)); if (msg.IndexOf("was deadlocked on lock resources") > 0) { //retry = true; } } } return(isValid); }
public bool DeleteAll(Entity parent, ref SaveStatus status) { bool isValid = true; //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ". Error - the provided target parent entity was not provided."); return(false); } try { using (var context = new EntityContext()) { var results = context.Entity_ProcessProfile.Where(s => s.EntityId == parent.Id) .ToList(); if (results == null || results.Count == 0) { return(true); } foreach (var item in results) { //21-03-31 mp - just removing the profile will not remove its entity and the latter's children! string statusMessage = ""; new EntityManager().Delete(item.RowId, string.Format("ProcessProfile: {0} for EntityType: {1} ({2})", item.Id, parent.EntityTypeId, parent.EntityBaseId), ref statusMessage); context.Entity_ProcessProfile.Remove(item); var count = context.SaveChanges(); if (count > 0) { } } //context.Entity_ProcessProfile.RemoveRange( context.Entity_ProcessProfile.Where( s => s.EntityId == parent.Id ) ); //int count = context.SaveChanges(); //if ( count > 0 ) //{ // isValid = true; //} //else //{ // //if doing a delete on spec, may not have been any properties //} } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg)); } return(isValid); }
} // /// <summary> /// Delete all properties for parent (in preparation for import) /// </summary> /// <param name="parent"></param> /// <param name="messages"></param> /// <returns></returns> public bool DeleteAll(Entity parent, ref SaveStatus status) { bool isValid = true; //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ". Error - the provided target parent entity was not provided."); return(false); } using (var context = new EntityContext()) { var results = context.Entity_ReferenceFramework .Where(s => s.EntityId == parent.Id) .OrderBy(s => s.CategoryId) .ThenBy(s => s.Created) .ToList(); if (results == null || !results.Any()) { return(true); } try { context.Entity_ReferenceFramework.RemoveRange(results); //context.Entity_ReferenceFramework.RemoveRange( context.Entity_ReferenceFramework.Where( s => s.EntityId == parent.Id ) ); //attempt to address DbUpdateConcurrencyException context.SaveChanges(); } catch (Exception cex) { isValid = false; // Update the values of the entity that failed to save from the store //don't want this //cex.Entries.Single().Reload(); //OR // Update original values from the database //Don't think this is what we want either. //var entry = cex.Entries.Single(); //entry.OriginalValues.SetValues( entry.GetDatabaseValues() ); var msg = BaseFactory.FormatExceptions(cex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, DbUpdateConcurrencyException: {2}", parent.EntityType, parent.EntityBaseId, msg)); } } return(isValid); }
public bool DeleteAll(Guid parentUid, ref SaveStatus status, DateTime?lastUpdated = null) { bool isValid = true; Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ". Error - the provided target parent entity was not provided."); return(false); } try { using (var context = new EntityContext()) { var results = context.Entity_AggregateDataProfile.Where(s => s.EntityId == parent.Id && (lastUpdated == null || s.LastUpdated < lastUpdated)).ToList(); if (results == null || results.Count == 0) { return(true); } foreach (var item in results) { //21-03-31 mp - just removing the profile will not remove its entity and the latter's children! //21-04-22 mp - we have a trigger for this //string statusMessage = ""; //delete the entity in order to remove Entity.DatasetProfile - but the datasetProfile doesn't get removed! //new EntityManager().Delete( item.RowId, string.Format( "AggregateDataProfile: {0} for EntityType: {1} ({2})", item.Id, parent.EntityTypeId, parent.EntityBaseId ), ref statusMessage ); context.Entity_AggregateDataProfile.Remove(item); var count = context.SaveChanges(); if (count > 0) { } } } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg)); } return(isValid); }
//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_CommonCost // .SingleOrDefault( s => s.EntityId == parent.Id && s.CostManifestId == profileId ); // if ( efEntity != null && efEntity.Id > 0 ) // { // context.Entity_CommonCost.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; //} public bool DeleteAll(Entity parent, ref SaveStatus status) { bool isValid = true; //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ". Error - the provided target parent entity was not provided."); return(false); } try { using (var context = new EntityContext()) { var results = context.Entity_CommonCost.Where(s => s.EntityId == parent.Id) .ToList(); if (results == null || results.Count == 0) { return(true); } context.Entity_CommonCost.RemoveRange(context.Entity_CommonCost.Where(s => s.EntityId == parent.Id)); int count = context.SaveChanges(); if (count > 0) { isValid = true; } else { //if doing a delete on spec, may not have been any properties } } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg)); } return(isValid); }
public bool DeleteAll(string pathwayCTID, ref SaveStatus status, DateTime?lastUpdated = null) { bool isValid = true; //Entity parent = EntityManager.GetEntity( parentUid ); if (string.IsNullOrWhiteSpace(pathwayCTID)) { status.AddError(thisClassName + ". Error - a pathwayCTID was not provided."); return(false); } try { using (var context = new EntityContext()) { var results = context.PathwayComponent.Where(s => s.PathwayCTID == pathwayCTID).ToList(); if (results == null || results.Count == 0) { return(true); } foreach (var item in results) { //21-03-31 mp - just removing the profile will not remove its entity and the latter's children! string statusMessage = ""; new EntityManager().Delete(item.RowId, string.Format("PathwayComponent: {0} ({1})", item.Name, item.Id), ref statusMessage); context.PathwayComponent.Remove(item); var count = context.SaveChanges(); if (count > 0) { } } } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. pathwayCTID: {0}, exception: {1}", pathwayCTID, msg)); } return(isValid); }
/// <summary> /// Delete all properties for parent (in preparation for import) /// </summary> /// <param name="parent"></param> /// <param name="messages"></param> /// <returns></returns> public bool DeleteAll(Entity parent, ref SaveStatus status) { bool isValid = true; //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ".DeleteAll Error - the provided target parent entity was not provided."); return(false); } int expectedDeleteCount = 0; try { using (var context = new EntityContext()) { var results = context.Entity_Reference.Where(s => s.EntityId == parent.Id) .ToList(); if (results == null || !results.Any()) { return(true); } expectedDeleteCount = results.Count; context.Entity_Reference.RemoveRange(results); int count = context.SaveChanges(); if (count > 0) { isValid = true; } else { //if doing a delete on spec, may not have been any properties } } } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException dbcex) { if (dbcex.Message.IndexOf("an unexpected number of rows (0)") > 0) { //don't know why this happens, quashing for now. LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. Parent type: {0}, ParentId: {1}, expectedDeletes: {2}. Message: {3}", parent.EntityTypeId, parent.EntityBaseId, expectedDeleteCount, dbcex.Message)); } else { var msg = BaseFactory.FormatExceptions(dbcex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, DbUpdateConcurrencyException: {2}", parent.EntityType, parent.EntityBaseId, msg)); } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg)); if (msg.IndexOf("was deadlocked on lock resources") > 0) { //retry = true; } } return(isValid); }
public bool DeleteAll(Entity parent, int attemptNbr, ref SaveStatus status, ref bool retry) { bool isValid = false; int expectedDeleteCount = 0; using (var context = new EntityContext()) { try { var results = context.Entity_CostProfile.Where(s => s.EntityId == parent.Id) .ToList(); if (results == null || results.Count == 0) { return(true); } expectedDeleteCount = results.Count; foreach (var item in results) { string statusMessage = ""; //we have a trigger for this //new EntityManager().Delete( item.RowId, string.Format("CostProfile: {0} for EntityType: {1} ({2})", item.Id, parent.EntityTypeId, parent.EntityBaseId), ref statusMessage ); context.Entity_CostProfile.Remove(item); int count = context.SaveChanges(); if (count > 0) { isValid = true; } else { //if doing a delete on spec, may not have been any properties } } //context.Entity_CostProfile.RemoveRange( context.Entity_CostProfile.Where( s => s.EntityId == parent.Id ) ); } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException dbcex) { if (dbcex.Message.IndexOf("an unexpected number of rows (0)") > 0) { //don't know why this happens, quashing for now. LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. Parent type: {0}, ParentId: {1}, expectedDeletes: {2}. Message: {3}", parent.EntityTypeId, parent.EntityBaseId, expectedDeleteCount, dbcex.Message)); isValid = true; } else { var msg = BaseFactory.FormatExceptions(dbcex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, DbUpdateConcurrencyException: {2}", parent.EntityType, parent.EntityBaseId, msg)); } } catch (Exception ex) { var msg = BaseFactory.FormatExceptions(ex); LoggingHelper.DoTrace(1, string.Format(thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg)); if (msg.IndexOf("was deadlocked on lock resources") > 0) { retry = true; } } } return(isValid); }