// добавление нового блюда в бд public void AddDish(Dish dish) { using (_context = new Context()) { if (dish.Category != null) _context.Categories.Attach(dish.Category); _context.Dishes.Add(dish); _context.SaveChanges(); } }
// создание и инициализация бд из текстовых файлов public DataBaseRepository() { using (_context = new Context()) { if (!_context.Database.Exists()) { _context.Database.Create(); var categories = LoadCategoriesFromFile(); _context.Categories.AddRange(categories); var dishes = LoadDishesFromFile(); _context.Dishes.AddRange(dishes); var history = LoadHistoryFromFile(); _context.EatingHistory.AddRange(history); _context.SaveChanges(); } } }
// добавление записи в таблицу истории public void AddEatingHistoryItem(Dish dish, float quantity) { EatingHistoryItem eatingHistoryItem = new EatingHistoryItem { Date = DateTime.Now.Date, Dish = dish, Quantity = quantity, Calories = dish.Calories / 100 * quantity, Fats = dish.Fats / 100 * quantity, Proteins = dish.Proteins / 100 * quantity, Carbohydrates = dish.Carbohydrates / 100 * quantity }; using (_context = new Context()) { _context.Dishes.Attach(dish); _context.EatingHistory.Add(eatingHistoryItem); _context.SaveChanges(); } }
// удаление записи из таблицы истории public void RemoveEatingHistoryItem(EatingHistoryItem item) { using (_context = new Context()) { var request = from eh in _context.EatingHistory where eh.ID == item.ID select eh; _context.EatingHistory.Remove(request.Single()); _context.SaveChanges(); } }