コード例 #1
0
        public static void Scrape(IApplicationBuilder applicationBuilder)
        {
            var scope = applicationBuilder.ApplicationServices.CreateScope();
            HistoryDbContext _historyDbContext = scope.ServiceProvider.GetRequiredService <HistoryDbContext>();
            PageScraper      pageScraper       = new PageScraper();
            var          months = CultureInfo.GetCultureInfo("en-us").DateTimeFormat.MonthNames;
            List <Event> events = new List <Event>();
            List <Birth> births = new List <Birth>();
            List <Death> deaths = new List <Death>();
            var          watch  = System.Diagnostics.Stopwatch.StartNew();

            if (!_historyDbContext.Event.Any())
            {
                for (int i = 0; i < 12; i++)
                {
                    for (int j = 1; j < DateTime.DaysInMonth(DateTime.Now.Year, i + 1); j++)
                    {
                        events = pageScraper.GetData <Event>(months[i] + "_" + j.ToString(), "1");
                        _historyDbContext.AddRange(events);
                    }
                }

                _historyDbContext.SaveChanges();
            }
            if (!_historyDbContext.Birth.Any())
            {
                for (int i = 0; i < 12; i++)
                {
                    for (int j = 1; j < DateTime.DaysInMonth(DateTime.Now.Year, i + 1); j++)
                    {
                        births = pageScraper.GetData <Birth>(months[i] + "_" + j.ToString(), "2");
                        _historyDbContext.AddRange(births);
                    }
                }

                _historyDbContext.SaveChanges();
            }

            if (!_historyDbContext.Death.Any())
            {
                for (int i = 0; i < 12; i++)
                {
                    for (int j = 1; j < DateTime.DaysInMonth(DateTime.Now.Year, i + 1); j++)
                    {
                        deaths = pageScraper.GetData <Death>(months[i] + "_" + j.ToString(), "3");
                        _historyDbContext.AddRange(deaths);
                    }
                }

                _historyDbContext.SaveChanges();
            }

            // measuring the time it takes to scrape for fun
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            System.Diagnostics.Trace.WriteLine("ScrapeData took" + elapsedMs.ToString() + " ms");
        }
コード例 #2
0
        protected OperationStatus SaveChanges()
        {
            var countChanges = 0;

            try
            {
                countChanges = context.SaveChanges();
            }
            catch (Exception e)
            {
                return(OperationStatus.Error);
            }

            return(countChanges > 0 ? OperationStatus.Success : OperationStatus.Silence);
        }
コード例 #3
0
 void IRepositoryInterface.create(History history)
 {
     historyDbContext.histories.Add(history);
     historyDbContext.SaveChanges(true);
 }
コード例 #4
0
 public void Save()
 {
     _context.SaveChanges();
 }