public int Update(Dictionary <RowHash, List <object> > rowData) { var idHashes = rowData.Keys.Select(rh => rh.IdHash); var dtosWithHashes = ReadWithHashes(rowData); var entities = DbSet .Where(x => idHashes.Contains(x.IdHash)) .ToList(); var updatedEntities = _mapper.MapOnto(entities, dtosWithHashes).ToList(); updatedEntities = AttachRelatedEntities(updatedEntities); DbContext.SaveChanges(); entities.ForEach(e => Reporter.ReportUpdated(Entity, e.IdHash, e.Id)); return(updatedEntities.Count()); }
public int Update(Dictionary <RowHash, List <object> > rowData) { var availabilities = _dbContext.PokemonAvailabilities.ToList(); var varieties = _dbContext.PokemonVarieties .IncludeOptimized(v => v.PokemonSpecies) .IncludeOptimized(v => v.Forms) .IncludeOptimized(v => v.DefaultForm) .IncludeOptimized(v => v.PrimaryType) .IncludeOptimized(v => v.SecondaryType) .IncludeOptimized(v => v.PrimaryAbility) .IncludeOptimized(v => v.SecondaryAbility) .IncludeOptimized(v => v.HiddenAbility) .IncludeOptimized(v => v.PvpTier) .IncludeOptimized(v => v.Urls) .ToList(); var idHashes = rowData.Keys.Select(rh => rh.IdHash); var dtosWithHashes = ReadWithHashes(rowData); var entities = _dbContext.PokemonForms .Where(x => idHashes.Contains(x.IdHash)) .ToList(); var updatedEntities = _mapper.MapOnto(entities, dtosWithHashes).ToList(); for (var i = 0; i < updatedEntities.Count; i++) { var entity = updatedEntities[i]; var availability = availabilities.SingleOrDefault(a => a.Name.EqualsExact(entity.Availability.Name)); if (availability is null) { _reporter.ReportError(Entity.PokemonForm, entity.IdHash, $"Could not find availability {entity.Availability.Name} for PokemonForm {entity.Name}, skipping."); entities.Remove(entity); i--; continue; } entity.AvailabilityId = availability.Id; entity.Availability = availability; var pokemonVariety = varieties.SingleOrDefault(s => s.Name.EqualsExact(entity.PokemonVariety.Name)); // If PokemonVariety already exists, update it (else add the new one) var variety = UpdatePokemonVariety(pokemonVariety, entity.PokemonVariety, entity.IdHash); if (variety is null) { entities.Remove(entity); i--; continue; } entity.PokemonVariety = variety; entity.PokemonVarietyId = variety.Id; } // Save default forms and default varieties, then remove them temporarily for initial insert. var defaultForms = entities .Select(f => f.PokemonVariety) .Distinct() .ToDictionary(v => v.Name, v => v.DefaultForm.Name); var defaultVarieties = entities .Select(f => f.PokemonVariety.PokemonSpecies) .Distinct() .ToDictionary(s => s.Name, s => s.DefaultVariety.Name); entities.Select(f => f.PokemonVariety).ToList().ForEach(v => v.DefaultForm = null); entities.Select(f => f.PokemonVariety.PokemonSpecies).ToList().ForEach(s => s.DefaultVariety = null); _dbContext.PokemonForms.UpdateRange(entities); _dbContext.SaveChanges(); // Attach Default form and default variety varieties = _dbContext.PokemonVarieties.ToList(); var forms = _dbContext.PokemonForms.ToList(); for (var i = 0; i < entities.Count; i++) { var entity = entities[i]; var defaultForm = forms.SingleOrDefault(f => f.Name.EqualsExact(defaultForms[entity.PokemonVariety.Name])); if (defaultForm is null) { _reporter.ReportError(Entity.PokemonForm, entity.IdHash, $"Could not find default form {entity.PokemonVariety.DefaultForm.Name} " + $"for PokemonForm {entity.Name}, skipping."); entities.Remove(entity); i--; continue; } entity.PokemonVariety.DefaultFormId = defaultForm.Id; entity.PokemonVariety.DefaultForm = defaultForm; var defaultVariety = varieties .SingleOrDefault(v => v.Name.EqualsExact(defaultVarieties[entity.PokemonVariety.PokemonSpecies.Name])); if (defaultVariety is null) { _reporter.ReportError(Entity.PokemonForm, entity.IdHash, $"Could not find default variety {entity.PokemonVariety.PokemonSpecies.DefaultVariety.Name} " + $"for PokemonForm {entity.Name}, skipping."); entities.Remove(entity); i--; continue; } entity.PokemonVariety.PokemonSpecies.DefaultVarietyId = defaultVariety.Id; entity.PokemonVariety.PokemonSpecies.DefaultVariety = defaultVariety; } _dbContext.SaveChanges(); DeleteOrphans(); entities.ForEach(e => _reporter.ReportUpdated(Entity.PokemonForm, e.IdHash, e.Id)); return(entities.Count); }