private static void DeleteExample() { using var context = new MASContext(); context.City.Remove(context.City.FirstOrDefault() ?? throw new InvalidOperationException()); context.SaveChanges(); }
public static void New() { using (var context = new MASContext()) { context.Menus.Add(new Menu(0.0d)); context.SaveChanges(); } }
private static void UpdateExample() { using var context = new MASContext(); var city = context.City.FirstOrDefaultAsync(); city.Result.Name = "Warszawa"; context.City.Update(city.Result); context.SaveChanges(); }
public static void Delete(Menu menu) { if (menu == null) { throw new Exception("Passed argument is null"); } using (var context = new MASContext()) { context.Menus.Remove(menu); context.SaveChanges(); } }
public static void AddIngredient(int dishId, double quantity, Ingredient ingredient) { using (var context = new MASContext()) { var dish = context.Dishes.Include(a => a.DishIngredients).Where(x => x.Id == dishId).FirstOrDefault(); if (dish != null && !dish.DishIngredients.Any(a => a.IngredientId == ingredient.Id)) { context.DishContents.Add(new DishContent(dish, ingredient, quantity)); context.SaveChanges(); } } }
public static void ChangePrices(Menu menu, double newMargin) { using (var context = new MASContext()) { menu.ProfitMargin = newMargin; context.Menus.Update(menu); context.SaveChanges(); using (var transaction = context.Database.BeginTransaction()) { var dishes = GetDishes(menu.Id); if (dishes != null) { foreach (var dish in dishes) { DishService.ChangePrice(dish, newMargin); } } transaction.Commit(); } context.SaveChanges(); } }
private static void AddExample() { using var context = new MASContext(); User person = new PremiumUser() { PasswordHash = "secret", Name = "Michal", CreatedAt = DateTime.Today, Email = "*****@*****.**" }; var city = new City { Name = "Piaseczno" }; city.User.Add(person); context.City.Add(city); context.SaveChanges(); }
public static void ChangePrice(Dish dish, double newMargin) { using (var context = new MASContext()) { double sum = 0.0; var contents = context.DishContents.Include(a => a.Ingredient).Where(a => a.DishId == dish.Id).ToList(); foreach (var content in contents) { var quantity = (double)content.Quantity; var pricePerKg = (double)content.Ingredient.PricePerKg; sum += quantity * pricePerKg; } sum += sum * newMargin; dish.Price = (Decimal)sum; Dish EditedDish = context.Dishes.Where(a => a.Id == dish.Id).FirstOrDefault(); EditedDish.Price = (Decimal)Math.Round(sum, 2); context.Update(EditedDish); context.SaveChanges(); } }