Beispiel #1
0
 /// <summary>
 /// Method that will be called on an entity immediately after the item is saved by context
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 public virtual void PostSaveChanges(Rock.Data.DbContext dbContext)
 {
     if (HistoryItems?.Any() == true)
     {
         if (HistoryItems.Any())
         {
             Task.Run(async() =>
             {
                 // Wait 1 second to allow all post save actions to complete
                 await Task.Delay(1000);
                 try
                 {
                     using (var rockContext = new RockContext())
                     {
                         rockContext.BulkInsert(HistoryItems);
                     }
                 }
                 catch (SystemException ex)
                 {
                     ExceptionLogService.LogException(ex, null);
                 }
             });
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Adds any missing person alternate ids; limited to 150k records per run
        /// to avoid any possible memory issues. Processes about 150k records
        /// in 52 seconds.
        /// </summary>
        private static void AddMissingAlternateIds()
        {
            using (var personRockContext = new Rock.Data.RockContext())
            {
                var personService          = new PersonService(personRockContext);
                int alternateValueId       = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_SEARCH_KEYS_ALTERNATE_ID.AsGuid()).Id;
                var personSearchKeyService = new PersonSearchKeyService(personRockContext);
                var alternateKeyQuery      = personSearchKeyService.Queryable().AsNoTracking().Where(a => a.SearchTypeValueId == alternateValueId);

                IQueryable <Person> personQuery = personService.Queryable(includeDeceased: true).AsNoTracking();

                // Make a list of items that we're going to bulk insert.
                var itemsToInsert = new List <PersonSearchKey>();

                // Get all existing keys so we can keep track and quickly check them while we're bulk adding new ones.
                var keys = new HashSet <string>(personSearchKeyService.Queryable().AsNoTracking()
                                                .Where(a => a.SearchTypeValueId == alternateValueId)
                                                .Select(a => a.SearchValue)
                                                .ToList());

                string alternateId = string.Empty;

                // Find everyone who does not yet have an alternateKey.
                foreach (var person in personQuery = personQuery
                                                     .Where(p => !alternateKeyQuery.Any(f => f.PersonAlias.PersonId == p.Id))
                                                     .Take(150000))
                {
                    // Regenerate key if it already exists.
                    do
                    {
                        alternateId = PersonSearchKeyService.GenerateRandomAlternateId();
                    } while (keys.Contains(alternateId));

                    keys.Add(alternateId);

                    itemsToInsert.Add(
                        new PersonSearchKey()
                    {
                        PersonAliasId     = person.PrimaryAliasId,
                        SearchTypeValueId = alternateValueId,
                        SearchValue       = alternateId
                    }
                        );
                }

                if (itemsToInsert.Count > 0)
                {
                    // Now add them in one bulk insert.
                    personRockContext.BulkInsert(itemsToInsert);
                }
            }
        }