public async Task SaveChanges() { var existingStocks = await dBContext.TrackedStocks.ToListAsync(); foreach (var dbstock in existingStocks) { if (PPE_Worker_Service.Worker.TrackedStocks.ContainsKey(dbstock.StockID)) { continue; } else { dBContext.Remove(dbstock); } } foreach (var stock in PPE_Worker_Service.Worker.TrackedStocks.Values) { if (existingStocks.FirstOrDefault(s => s.StockID == stock.StockID) != null) { continue; } else { dBContext.Add(new PPE_Models.TrackedStock() { StockID = stock.StockID }); } } await dBContext.SaveChangesAsync(); }
public void Save(string participantID, string testName, string bed, string wake, string latency, string tst) { var model = _dbContext.SleepQuestions.SingleOrDefault(x => string.Equals(x.ParticipantID, participantID)) ?? new SleepQuestionsDbModel { ParticipantID = participantID }; var bedProperty = model.GetType().GetProperty($"{testName}Bed") ?? throw new Exception($"Unexpected test name."); bedProperty.SetValue(model, bed, null); var wakeProperty = model.GetType().GetProperty($"{testName}Wake") ?? throw new Exception($"Unexpected test name."); wakeProperty.SetValue(model, wake, null); var latencyProperty = model.GetType().GetProperty($"{testName}Latency") ?? throw new Exception($"Unexpected test name."); latencyProperty.SetValue(model, latency, null); var tstProperty = model.GetType().GetProperty($"{testName}TST") ?? throw new Exception($"Unexpected test name."); tstProperty.SetValue(model, tst, null); if (_dbContext.SleepQuestions.Any(x => string.Equals(participantID, x.ParticipantID))) { _dbContext.Update(model); } else { _dbContext.Add(model); } _dbContext.SaveChanges(); }
public void Save(PSQIDbModel psqi) { if (_dbContext.PSQIs.Any(x => string.Equals(psqi.ParticipantID, x.ParticipantID))) { _dbContext.Update(psqi); } else { _dbContext.Add(psqi); } _dbContext.SaveChanges(); }
public void Save(ConsentDbModel consent) { if (_dbContext.Consents.Any(x => string.Equals(consent.ParticipantID, x.ParticipantID))) { _dbContext.Update(consent); } else { _dbContext.Add(consent); } _dbContext.SaveChanges(); }
public void Save(SessionMetaDbModel sessionMeta) { if (_dbContext.SessionMetas.Any(x => string.Equals(sessionMeta.ParticipantID, x.ParticipantID) && string.Equals(sessionMeta.SessionName, x.SessionName))) { _dbContext.Update(sessionMeta); } else { _dbContext.Add(sessionMeta); } _dbContext.SaveChanges(); }
public void Save(DemographicsDbModel demographics) { if (_dbContext.Demographics.Any(x => string.Equals(demographics.ParticipantID, x.ParticipantID))) { _dbContext.Update(demographics); } else { _dbContext.Add(demographics); } _dbContext.SaveChanges(); }
public void Save(EpworthDbModel epworth) { if (_dbContext.Epworths.Any(x => string.Equals(epworth.ParticipantID, x.ParticipantID))) { _dbContext.Update(epworth); } else { _dbContext.Add(epworth); } _dbContext.SaveChanges(); }
public void Save(string participantID, string testName, Sleepinesses?stanford) { var stanfordModel = _dbContext.Stanfords.SingleOrDefault(x => string.Equals(x.ParticipantID, participantID)) ?? new StanfordDbModel { ParticipantID = participantID }; var testNameProperty = stanfordModel.GetType().GetProperty(testName) ?? throw new Exception($"Unexpected test name."); testNameProperty.SetValue(stanfordModel, stanford, null); var utcProperty = stanfordModel.GetType().GetProperty($"{testName}Utc") ?? throw new Exception($"Unexpected test name."); utcProperty.SetValue(stanfordModel, DateTime.UtcNow, null); if (_dbContext.Stanfords.Any(x => string.Equals(participantID, x.ParticipantID))) { _dbContext.Update(stanfordModel); } else { _dbContext.Add(stanfordModel); } _dbContext.SaveChanges(); }
/// <summary> /// Load data from OrderItem into OrderItemSQLite /// </summary> private void AddOrderItem() { Console.WriteLine("Loading data into SQLite >>> OrderItem"); context.OrderItem.Load(); foreach (OrderItem item in context.OrderItem.Local.ToList()) { contextSQLite.Add(new OrderItemSQLite { OrderItemId = item.OrderItemId, OrderId = item.OrderId, ProductId = item.ProductId, Quantity = item.Quantity }); } contextSQLite.SaveChanges(); }
public void Save(ResponseDbDataModel model) { _dbContext.Add(model); _dbContext.SaveChanges(); }
public void Add_writes_to_database() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <SQLiteDBContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new SQLiteDBContext(options)) { context.Database.EnsureCreated(); } using (var context = new SQLiteDBContext(options)) { LootSplit lootsplit = new LootSplit { Name = "Test Loot Split", LootItemId = 1, LootItems = new List <LootItem> { new LootItem { Name = "test loot item", Price = 5, Description = "test" }, new LootItem { Name = "test loot item 2", Price = 10, Description = "test2" } } }; LootItem lootitem = new LootItem { Name = "Test Loot Item", Price = 20, LootSplits = new List <LootSplit> { new LootSplit { Name = "Loot Split Test 3", LootItemId = 2, }, new LootSplit { Name = "Loot Split 4", LootItemId = 3 } } }; context.Add(lootitem); context.Add(lootsplit); context.SaveChanges(); } using (var context = new SQLiteDBContext(options)) { Assert.Equals(2, context.LootSplit.CountAsync()); } } finally { connection.Close(); } }