private void SaveExecute() { try { Ingredients = GetAllIngredients(); using (RecipeDatabaseEntities db = new RecipeDatabaseEntities()) { tblRecipe oldRecipe = new tblRecipe(); oldRecipe = db.tblRecipes.Where(r => r.RecipeID == Recipe.RecipeID).FirstOrDefault(); oldRecipe.RecipeName = Recipe.RecipeName; oldRecipe.Portions = Recipe.Portions; oldRecipe.RecipeType = Type; oldRecipe.RecipeDateOfCreation = DateTime.Now; oldRecipe.RecipeText = AllIngredientsToString(Ingredients); if (oldRecipe.RecipeText == "") { MessageBox.Show("Please choose ingredients."); return; } else { if (User.UserName == "Admin") { oldRecipe.UserID = User.UserID; } if (Ingredients != null) { foreach (tblIngredient ingredient in Ingredients) { tblRecipeIngredient recipeIngredient = new tblRecipeIngredient(); recipeIngredient.RecipeID = Recipe.RecipeID; recipeIngredient.IngredientID = ingredient.IngredientID; db.tblRecipeIngredients.Add(recipeIngredient); } } db.SaveChanges(); } } MessageBox.Show("Recipe Updated Successfully!"); updateView.Close(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
public static void Delete(int recipeid, int ingredientid, int measureid) { try { // Create new instance of ChefsCornerEntities/ Connection string ChefsCornerEntities dc = new ChefsCornerEntities(); tblRecipeIngredient rin = dc.tblRecipeIngredients.FirstOrDefault(rci => rci.rc_Id == recipeid && rci.in_Id == ingredientid && rci.ms_Id == measureid); if (rin != null) { dc.tblRecipeIngredients.Remove(rin); dc.SaveChanges(); } } catch (Exception e) { throw e; } }
public static void Add(int recipeid, int ingredientid, int measureid) { try { // Create new instance of ChefsCornerEntities ChefsCornerEntities dc = new ChefsCornerEntities(); // Create new instance of tblRecipeIngredient tblRecipeIngredient rin = new tblRecipeIngredient(); // Add new Ids for recipes, ingredients, and measures to tblRecipeIngredients. Otherwise, throw an exception. rin.ri_Id = dc.tblRecipeIngredients.Any() ? dc.tblRecipeIngredients.Max(r => r.ri_Id) + 1 : 1; rin.in_Id = ingredientid; rin.rc_Id = recipeid; rin.ms_Id = measureid; dc.tblRecipeIngredients.Add(rin); dc.SaveChanges(); } catch (Exception e) { throw e; } }
private void SaveExecute() { try { Ingredients = GetAllIngredients(); Recipe.RecipeType = Type; Recipe.RecipeDateOfCreation = DateTime.Now; Recipe.RecipeText = AllIngredientsToString(Ingredients); if (Recipe.RecipeText == "") { MessageBox.Show("Please choose ingredients."); return; } using (RecipeDatabaseEntities db = new RecipeDatabaseEntities()) { Recipe.UserID = User.UserID; db.tblRecipes.Add(Recipe); db.SaveChanges(); foreach (tblIngredient ingredient in Ingredients) { tblRecipeIngredient recipeIngredient = new tblRecipeIngredient(); recipeIngredient.RecipeID = Recipe.RecipeID; recipeIngredient.IngredientID = ingredient.IngredientID; db.tblRecipeIngredients.Add(recipeIngredient); } db.SaveChanges(); } MessageBox.Show("Recipe Created Successfully!"); addRecipe.Close(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }