public MainFood UpdateMainFood(MainFood foodUpdate) { List <RecipeLine> newRecipeLines = new List <RecipeLine>(); List <AllergensInMenu> newAllegensMenu = new List <AllergensInMenu>(); if (foodUpdate.RecipeLines != null) { newRecipeLines = new List <RecipeLine>(foodUpdate.RecipeLines); } if (foodUpdate.AllergensInMenu != null) { newAllegensMenu = new List <AllergensInMenu>(foodUpdate.AllergensInMenu); } _ctx.Attach(foodUpdate).State = EntityState.Modified; _ctx.RecipeLine.RemoveRange( _ctx.RecipeLine.Where(m => m.MainFoodId == foodUpdate.Id) ); _ctx.AllergensInMenu.RemoveRange( _ctx.AllergensInMenu.Where(ol => ol.MainFoodId == foodUpdate.Id) ); foreach (var RL in newRecipeLines) { _ctx.Entry(RL).State = EntityState.Added; } foreach (var AM in newAllegensMenu) { _ctx.Entry(AM).State = EntityState.Added; } // Save it _ctx.SaveChanges(); return(foodUpdate); }
public Allergens CreateAllergen(Allergens allergen) { //Clone allergensInMenu to new location in memory, so they are not overridden on Attach var newAllergenInMenu = new List <AllergensInMenu>(allergen.AllergensInMenu); //Attach ingredient so basic properties are updated _ctx.Attach(allergen).State = EntityState.Added; //Remove all recipelines with updated order information _ctx.AllergensInMenu.RemoveRange( _ctx.AllergensInMenu.Where(ol => ol.MainFoodId == allergen.Id) ); //Add all orderlines with updated order information foreach (var ol in newAllergenInMenu) { _ctx.Entry(ol).State = EntityState.Added; } // Save it _ctx.SaveChanges(); //Return it return(allergen); }
public Ingredients UpdateIngredient(Ingredients ingredientUpdate) { foreach (var item in _ctx.RecipeLine.ToList().Where(c => c.IngredientsId == ingredientUpdate.Id)) { _ctx.RecipeLine.Remove(item); } //Clone orderlines to new location in memory, so they are not overridden on Attach var newRecipeLines = new List <RecipeLine>(ingredientUpdate.RecipeLines); //Attach order so basic properties are updated _ctx.Attach(ingredientUpdate).State = EntityState.Modified; //Add all orderlines with updated order information foreach (var ol in newRecipeLines) { _ctx.Entry(ol).State = EntityState.Added; } // Save it _ctx.SaveChanges(); //Return it return(ingredientUpdate); }