/// <summary> /// Zapis wartości do bazy danych. /// </summary> public void AddNewElementsToDataBase() { foreach (var element in counter) { if (element.NewValue != String.Empty) { Reading newRecord = new Reading(); newRecord.CollectorId = this.collectorId; newRecord.Date = DateTime.Now; newRecord.CounterNo = Convert.ToInt32(element.CounterNo); newRecord.Value = Convert.ToDouble(element.NewValue); newRecord.ReadingId = Guid.NewGuid(); newRecord.InsertIntoDB(); } } }
/// <summary> /// Generuje losowo odczyty. /// </summary> static void GenerateReadings() { Random rnd = new Random(); Reading reading; Guid g; using (var dataBase = new CollectorsManagementSystemEntities()) { for (int i = 0; i < numberOfReadings; i++) { reading = new Reading(); do { g = Guid.NewGuid(); } while (g == Guid.Empty); reading.ReadingId = g;// Guid.NewGuid(); reading.CounterNo = Convert.ToInt32(ChooseRandomId(0)); //var counters = (from counter in dataBase.Counters // select counter.CounterNo).ToList(); List<int> test = (from c in dataBase.Counters select c.CounterNo).ToList(); var tmp = (from c in dataBase.Counters where c.CounterNo == reading.CounterNo select c.AddressId).FirstOrDefault(); var tmp2 = (from a in dataBase.Addresses where a.AddressId == tmp select a.AreaId).FirstOrDefault(); reading.CollectorId = (from a in dataBase.Areas where a.AreaId == tmp2 select a.CollectorId).FirstOrDefault().ToString(); var friendsOfReading = from r in dataBase.Readings where r.CounterNo == reading.CounterNo orderby r.Date descending select r; if (friendsOfReading.Count() == 0) reading.Value = rnd.Next(0, 1000); else { reading.Value = friendsOfReading.FirstOrDefault().Value + rnd.Next(0, 1000); foreach (Reading friendOfReading in friendsOfReading) friendOfReading.Date = friendOfReading.Date.AddDays(-30); dataBase.SaveChanges(); } reading.Date = DateTime.Now; reading.InsertIntoDB(); } } }