public async Task <int> Upload(string uploadFile) { if (uploadFile == null) { return(await Task.FromResult(0)); } //clear previous items for this app. foreach (var item in _wellDbContext.Tanks) { _wellDbContext.Tanks.Remove(item); } foreach (var item in _wellDbContext.Wells) { _wellDbContext.Wells.Remove(item); } await _wellDbContext.SaveChangesAsync(); //pick appropriate import strategy based on file extension var strategy = _importStrategy(uploadFile); if (strategy == null) { return(await Task.FromResult(0)); } var wells = strategy.Import(uploadFile); //save well data await _wellDbContext.Wells.AddRangeAsync(wells); return(await _wellDbContext.SaveChangesAsync()); }
/// <summary> /// Save tanks /// </summary> /// <param name="tanks">Number saved</param> /// <returns></returns> public async Task <int> Save(IEnumerable <TankModel> tanks) { var ids = tanks.Select(x => x.Id); var entities = _wellDbContext.Tanks.Where(x => ids.Contains(x.Id)).ToArray(); var entityDict = entities.ToDictionary(x => x.Id); foreach (var tank in tanks) { if (entityDict.ContainsKey(tank.Id)) { CopyToEntity(entityDict[tank.Id], tank); } } _wellDbContext.Tanks.UpdateRange(entityDict.Select(x => x.Value).ToArray()); return(await _wellDbContext.SaveChangesAsync()); }