public void IngestAll(SolarRadiationDataContext dbContext, IEnumerable <SolradNwpForecast> parsedForecastData,
                              Action <Exception> onError = null,
                              ulong saveBatchSize        = 100,
                              bool disableTracking       = true)
        {
            ulong count = 0;

            // This speeds up bulk inserts:
            if (disableTracking)
            {
                dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
            }

            foreach (var forecast in parsedForecastData)
            {
                try
                {
                    count = IngestSingle(forecast, dbContext, count, true);
                    if (count % saveBatchSize == 0)
                    {
                        SaveBatch(dbContext, count, onError, disableTracking);
                    }
                }
                catch (Exception e)
                {
                    onError?.Invoke(e);
                }
            }

            // Save last batch:
            SaveBatch(dbContext, count, onError, disableTracking);
        }
        public ulong IngestSingle(SolradNwpForecast ingested, SolarRadiationDataContext dbContext, ulong count, bool saveChanges = true)
        {
            var location = new LocationForecasts(ingested);

            dbContext.Locations.Add(location);
            if (saveChanges)
            {
                dbContext.SaveChanges();

                foreach (var f in dbContext.Forecasts)
                {
                    f.LocationForecastId = location.Id;
                }
                dbContext.SaveChanges();
            }
            return(count + 1);
        }
 private static void SaveBatch(SolarRadiationDataContext dbContext, ulong count, Action <Exception> onError, bool disableTracking)
 {
     try
     {
         if (disableTracking)
         {
             dbContext.ChangeTracker.AutoDetectChangesEnabled = true;
         }
         dbContext.SaveChanges();
         Console.WriteLine($"Processed {count} rows");
     }
     catch (Exception e)
     {
         onError?.Invoke(e);
     }
     if (disableTracking)
     {
         dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
     }
 }
Exemplo n.º 4
0
 public GeoLocator(SolarRadiationDataContext dataContext, uint srid = 0)
 {
     _dataContext       = dataContext ?? throw new ArgumentNullException(nameof(dataContext));
     _coordinateFactory = new CoordinateFactory(srid);
 }