public void ResetDb() { using (var db = new PricingContext()) { if (db.Stores.Any()) { var filteredNewItems = db.Items.Where(i => !db.HistoryItems.Any(hist => hist.ChainID == i.ChainID && hist.StoreID == i.StoreID && i.ItemCode == hist.ItemCode && i.ItemType == hist.ItemType && DateTime.Compare(hist.LastUpdateDate, i.LastUpdateDate) >= 0)).Select(i => new { ChainID = i.ChainID, StoreID = i.StoreID, ItemCode = i.ItemCode, ItemType = i.ItemType, LastUpdateDate = i.LastUpdateDate, Price = i.Price }).ToList(); var convertedToHistItems = filteredNewItems.Select(i => new HistoryItem() { ChainID = i.ChainID, StoreID = i.StoreID, ItemCode = i.ItemCode, ItemType = i.ItemType, LastUpdateDate = i.LastUpdateDate, Price = i.Price }).ToList(); db.HistoryItems.AddRange(convertedToHistItems); db.Stores.RemoveRange(db.Stores); db.SaveChanges(); } } }
public void WriteToDb() { using (var db = new PricingContext()) { db.Stores.AddRange(Stores); db.SaveChanges(); } }
public void WriteToDb() { using (var db = new PricingContext()) { var distinctItems = Items.GroupBy(i => new { i.ChainID, i.StoreID, i.ItemCode, i.ItemType }).Select(g => g.FirstOrDefault()); db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; db.Items.AddRange(distinctItems); db.SaveChanges(); } }
public bool AddAccount(string user, string password) { using (var db = new PricingContext()) { if (!db.Accounts.Any(a => a.UserID == user)) { Account newAccount = new Account(user, password); db.Accounts.Add(newAccount); db.SaveChanges(); return(true); } return(false); } }
public JsonResult PostPriceInfoJson([FromBody] PriceInfo priceInfo) { if (priceInfo == null) { return(Json(_context.PriceInfo.ToList())); // return BadRequest(); } if (priceInfo != null) { _context.PriceInfo.Add(priceInfo); _context.SaveChanges(); } return(Json(new { state = 0, msg = "Successfully Added Rows" })); }
public void WriteToDbPartial() { using (var db = new PricingContext()) { foreach (var item in Items) { Item currItem = db.Items.Find(item.StoreID, item.ChainID, item.ItemCode, item.ItemType); if (currItem != null) { HistoryItem histItem = db.HistoryItems.Find(currItem.StoreID, currItem.ChainID, currItem.ItemCode, currItem.ItemType, currItem.LastUpdateDate); if (histItem == null) { db.HistoryItems.Add(new HistoryItem(currItem.StoreID, currItem.ChainID, currItem.ItemCode, currItem.ItemType, currItem.Price, currItem.LastUpdateDate)); } db.Entry(currItem).CurrentValues.SetValues(item); } else { db.Items.Add(item); } } db.SaveChanges(); } }