public static void DBInit(AccountancyContext context) { context.Database.EnsureCreated(); if (context.Households.Any()) { return; } var households = new HouseholdModel[] { new HouseholdModel { ID = 500 }, new HouseholdModel { ID = 1000 }, new HouseholdModel { ID = 1500 } }; foreach (HouseholdModel Hm in households) { context.Households.Add(Hm); } context.Database.OpenConnection(); try { context.Database.ExecuteSqlRaw(@"SET IDENTITY_INSERT HouseholdModel ON"); context.SaveChanges(); context.Database.ExecuteSqlRaw(@"SET IDENTITY_INSERT HouseholdModel OFF"); } finally { context.Database.CloseConnection(); } var UtilityBills = new AccountancyInfo[] { new AccountancyInfo { HouseholdModelID = 500, BillCategory = "Water", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 500, BillCategory = "Heat", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 500, BillCategory = "Electricity", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 1000, BillCategory = "Water", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 1000, BillCategory = "Heat", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 1000, BillCategory = "Electricity", NetVal = 500, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 1500, BillCategory = "Water", NetVal = 0, TimestampDateTime = DateTime.Now }, new AccountancyInfo { HouseholdModelID = 1500, BillCategory = "Heat", NetVal = 500, TimestampDateTime = DateTime.Now } }; foreach (AccountancyInfo Ai in UtilityBills) { context.BillingInfo.Add(Ai); } context.SaveChanges(); }
public Task Handle(AccountancyRelay @event) { _log.Debug($"Entering Handler with event - ID: {@event.HouseID}, Netto Value {@event.NetVal}, Timestamp {@event.Timestamp}, Type: {@event.Type}"); try { if (@event.HouseID != 0) { _log.Debug("Entering if-statement(@event.HouseID): " + @event.HouseID); HouseholdModel House = new HouseholdModel { ID = @event.HouseID }; List <HouseholdModel> HouseList = _context.Households.ToList(); bool exists = false; foreach (HouseholdModel Hm in HouseList) { if (Hm.ID == @event.HouseID) { exists = true; } } Console.WriteLine($"Received message with netto value {@event.NetVal}"); if (exists) { _log.Debug("Entering Not-Found statement"); _context.Households.Add(House); _context.Database.OpenConnection(); try { _context.Database.ExecuteSqlRaw(@"SET IDENTITY_INSERT HouseholdModel ON"); _context.SaveChanges(); _context.Database.ExecuteSqlRaw(@"SET IDENTITY_INSERT HouseholdModel OFF"); } finally { _context.Database.CloseConnection(); } } var accountingInfo = new AccountancyInfo { HouseholdModelID = @event.HouseID, BillCategory = @event.Type, NetVal = @event.NetVal, TimestampDateTime = @event.Timestamp }; _context.BillingInfo.Add(accountingInfo); _context.SaveChanges(); } _log.Debug("Returning from MessageReceivedHandler"); return(Task.CompletedTask); } catch (Exception exception) { _log.Error("Failed with exception: " + exception); throw; } }