public void SaveData(Details details) { using (var context = new CentralHubDb()) { var site = context.GridBankSites.Find(details.SiteId); if (site == null) { throw new Exception("Site not found!"); } foreach (var entry in details.UsageEntries) { //See if this Guid already exists var currentRecord = site.Usages.FirstOrDefault(x => x.IdGuid == entry.IdGuid && x.GridBankSiteId == details.SiteId); if (currentRecord != null) { //Yes, it exists ... update it currentRecord.TimeStamp = entry.TimeStamp; currentRecord.CurrentPower = entry.CurrentPower; currentRecord.IsNew = true; } else { //No, this is a new entry ... add it site.Usages.Add(new Usage { IdGuid = entry.IdGuid, GridBankSiteId = details.SiteId, TimeStamp = entry.TimeStamp, CurrentPower = entry.CurrentPower, IsNew = true }); } } //Save everything context.SaveChanges(); } }
public static void Create() { using (var context = new CentralHubDb()) { context.GridBankSites.AddOrUpdate(x => x.Id, new GridBankSite { Id = 1, Description = "Hwy49-Harrisburg", ApiUrl = "http://localhost:62056/", IsActive = true }, new GridBankSite { Id = 2, Description = "Hwy29-Concord", ApiUrl = "http://localhost:62056/", IsActive = true } ); context.SaveChanges(); } }