private void DoEquipmentAudit(List <SeniorityAudit> audits, EntityEntry entry, string smUserId) { Equipment changed = (Equipment)entry.Entity; int tempChangedId = changed.Id; // if this is an "empy" record - exit if (tempChangedId <= 0) { return; } Equipment original = Equipments.AsNoTracking() .Include(x => x.LocalArea) .Include(x => x.Owner) .First(a => a.Id == tempChangedId); // compare the old and new if (changed.IsSeniorityAuditRequired(original)) { DateTime currentTime = DateTime.UtcNow; // create the audit entry. SeniorityAudit seniorityAudit = new SeniorityAudit { BlockNumber = original.BlockNumber, EndDate = currentTime }; int tempLocalAreaId = original.LocalArea.Id; int tempOwnerId = original.Owner.Id; changed.SeniorityEffectiveDate = currentTime; seniorityAudit.AppCreateTimestamp = currentTime; seniorityAudit.AppLastUpdateTimestamp = currentTime; seniorityAudit.AppCreateUserid = smUserId; seniorityAudit.AppLastUpdateUserid = smUserId; seniorityAudit.EquipmentId = tempChangedId; seniorityAudit.LocalAreaId = tempLocalAreaId; seniorityAudit.OwnerId = tempOwnerId; if (seniorityAudit.Owner != null) { seniorityAudit.OwnerOrganizationName = seniorityAudit.Owner.OrganizationName; } if (original.SeniorityEffectiveDate != null) { seniorityAudit.StartDate = (DateTime)original.SeniorityEffectiveDate; } seniorityAudit.Seniority = original.Seniority; seniorityAudit.ServiceHoursLastYear = original.ServiceHoursLastYear; seniorityAudit.ServiceHoursTwoYearsAgo = original.ServiceHoursTwoYearsAgo; seniorityAudit.ServiceHoursThreeYearsAgo = original.ServiceHoursThreeYearsAgo; audits.Add(seniorityAudit); } }