/// <summary> /// Cleans up expired entity sets. /// </summary> /// <param name="dataMap">The data map.</param> private void CleanupExpiredEntitySets(JobDataMap dataMap) { var entitySetRockContext = new Rock.Data.RockContext(); var currentDateTime = RockDateTime.Now; var entitySetService = new EntitySetService(entitySetRockContext); var qry = entitySetService.Queryable().Where(a => a.ExpireDateTime.HasValue && a.ExpireDateTime < currentDateTime); foreach (var entitySet in qry.ToList()) { string deleteWarning; if (entitySetService.CanDelete(entitySet, out deleteWarning)) { // delete in chunks (see http://dba.stackexchange.com/questions/1750/methods-of-speeding-up-a-huge-delete-from-table-with-no-clauses) bool keepDeleting = true; while (keepDeleting) { var dbTransaction = entitySetRockContext.Database.BeginTransaction(); try { string sqlCommand = @"DELETE TOP (1000) FROM [EntitySetItem] WHERE [EntitySetId] = @entitySetId"; int rowsDeleted = entitySetRockContext.Database.ExecuteSqlCommand(sqlCommand, new SqlParameter("entitySetId", entitySet.Id)); keepDeleting = rowsDeleted > 0; } finally { dbTransaction.Commit(); } } entitySetService.Delete(entitySet); entitySetRockContext.SaveChanges(); } } }
/// <summary> /// Handles the DeleteClick event of the gCampaigns control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void gCampaigns_DeleteClick(object sender, RowEventArgs e) { var campaignConnectionItem = CampaignConnectionHelper.GetCampaignConfiguration(e.RowKeyValue.ToString().AsGuid()); if (campaignConnectionItem != null && campaignConnectionItem.EntitySetId != default(int)) { var rockContext = new RockContext(); var entitySetService = new EntitySetService(rockContext); var entitySet = entitySetService.Get(campaignConnectionItem.EntitySetId); string errorMessage; if (!entitySetService.CanDelete(entitySet, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } var entitySetItemQry = new EntitySetItemService(rockContext) .Queryable().AsNoTracking() .Where(i => i.EntitySetId == entitySet.Id); rockContext.BulkDelete(entitySetItemQry); entitySetService.Delete(entitySet); rockContext.SaveChanges(); CampaignConnectionHelper.RemoveCampaignConfiguration(e.RowKeyValue.ToString().AsGuid()); BindGrid(); } }
/// <summary> /// Handles the Delete event of the gList control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void gList_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var entitySetService = new EntitySetService(rockContext); EntitySet entitySet = entitySetService.Get(e.RowKeyId); if (entitySet != null) { string errorMessage; if (!entitySetService.CanDelete(entitySet, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } // mark it as expired and RockCleanup will delete it later entitySet.ExpireDateTime = RockDateTime.Now.AddMinutes(-1); entitySet.EntitySetPurposeValueId = null; rockContext.SaveChanges(); } BindGrid(); }