public void BulkUpdate() { string url = "http://localhost:3000/api/scrape/week/this"; string jsonData = CallRestMethod(url); JObject calendar = JObject.Parse(jsonData); IList <JToken> results = calendar["calendarData"].Children().ToList(); IList <IndicatorData> recs = new List <IndicatorData>(); foreach (JToken result in results) { // JToken.ToObject is a helper method that uses JsonSerializer internally IndicatorData rec = result.ToObject <IndicatorData>(); recs.Add(rec); } using (var ctx = new ntpContext()) { foreach (var rec in recs) { var exist = ctx.IndicatorData.Where(r => r.EventId == rec.EventId).FirstOrDefault(); if (exist == null) { rec.CreateDate = DateTime.Now; ctx.IndicatorData.Add(rec); } else { exist.ModifyDate = DateTime.Now; exist.Actual = rec.Actual; exist.Forecast = rec.Forecast; exist.Indicator = rec.Indicator; exist.Previous = rec.Previous; exist.ReleaseDate = rec.ReleaseDate; exist.ReleaseDateTime = rec.ReleaseDateTime; exist.ReleaseTime = rec.ReleaseTime; ctx.Entry(exist).State = EntityState.Modified; } } ctx.SaveChanges(); } }
public void FixBadDates() { using (var ctx = new ntpContext()) { var dt = new DateTime(2020, 12, 30); var recs = (from r in ctx.IndicatorData where r.ReleaseDateTime >= dt select r).ToList(); foreach (var rec in recs) { var exist = ctx.IndicatorData.Where(r => r.EventId == rec.EventId).FirstOrDefault(); exist.Actual = rec.Actual; exist.Forecast = rec.Forecast; exist.Indicator = rec.Indicator; exist.Previous = rec.Previous; exist.ReleaseDate = rec.ReleaseDate.Replace("2021", "2020"); exist.ReleaseDateTime = rec.ReleaseDateTime.AddYears(-1); exist.ReleaseTime = rec.ReleaseTime; ctx.Entry(exist).State = EntityState.Modified; } ctx.SaveChanges(); } }