public static async Task <bool> AddDescriptionsToDataBaseAsync(DescDBContext dbContext, ICollection <LithologicDescription> descriptions) { try { dbContext.AttachRange(descriptions); await dbContext.SaveChangesAsync(); return(true); } catch (Exception ex) { throw new Exception("Error adding descriptions to database context."); } }
public static async Task <bool> AddMeasurementsToDataBaseAsync(DescDBContext dbContext, ICollection <Measurement> measurements) { try { dbContext.AttachRange(measurements); await dbContext.SaveChangesAsync(); return(true); } catch (Exception ex) { Log.Warning(ex.Message); throw new Exception("Error adding measurements to database context."); } }
private static async Task <bool> GetMeasurementsForSubIntervals(int skip, int take) { Console.Clear(); using (DescDBContext dbContext = new DescDBContext()) { var measurements = dbContext.MeasurementDescriptions .Where(x => x.ID > 14221456 - 1) //Enable this to start attributing descriptions after a certain ID, it's useful if adding new measurements .Include(x => x.SectionInfo) .Include(x => x.LithologicSubintervals) .Skip(skip) .Take(take) .Select(x => x) .ToHashSet(); foreach (var measurement in measurements) { Console.WriteLine(string.Format("Measurement ID: {0}", measurement.ID.ToString())); List <LithologicSubinterval> matchingSubintervals; try { matchingSubintervals = await dbContext.LithologicSubintervals .Include(x => x.SectionInfo) .Where(subinterval => subinterval.SectionInfo.ID == measurement.SectionInfo.ID) .Where(description => (measurement.StartOffset >= description.StartOffset && measurement.StartOffset <= description.EndOffset) || //Finds an interval in which the measurement falls in part (measurement.EndOffset >= description.StartOffset && measurement.EndOffset <= description.EndOffset)) .ToListAsync().ConfigureAwait(true); } catch (Exception ex) { Console.WriteLine("Error finding subintervals for measurement"); continue; } if (matchingSubintervals.Count == 0) { continue; } try { foreach (var subinterval in matchingSubintervals) { //Add only if it doesn't contain the subinterval if (!measurement.LithologicSubintervals.Contains(subinterval)) { measurement.LithologicSubintervals.Add(subinterval); Console.WriteLine(string.Format("Added Subinterval ID: {0}", subinterval.ID.ToString())); } } } catch (Exception ex) { Console.WriteLine(string.Format("Error adding Subintervals for Measurement: {0}", measurement.ID.ToString())); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } ; await dbContext.SaveChangesAsync().ConfigureAwait(true); } return(true); }