public void SetIsInMenu(bool isInMenu) { if (this.IsInMenu != isInMenu) { using (var context = new Model.Entities.RecipeDbContext()) { if (!this.IsInMenu) { context.MenuItems.Add(new MenuItem { Id = context.GenerateNewId(), RecipeId = this.Id, Portion = 1, CurrentStepIndex = 2 }); context.SaveChanges(); } else { var menuItem = (from menu in context.MenuItems where menu.RecipeId == this.Id select menu).FirstOrDefault(); if (menuItem != null) { context.MenuItems.Remove(menuItem); context.SaveChanges(); } } this._isInMenu = isInMenu; } } }
public void LoadDetails() { // make sure it is initialized only once if (!this._isFullyLoaded) { this.Steps.Clear(); this.Ingredients.Clear(); using (var context = new Model.Entities.RecipeDbContext()) { // load steps var steps = from step in context.RecipeSteps where step.RecipeId == this.Id orderby step.Index ascending select step; foreach (var step in steps) { this.Steps.Add(new RecipeStepViewModel(step)); } // load ingredients var ingredientNodes = context.RecipeIngredientRelations .Where(r => r.RecipeId == this.Id) .Join( context.Ingredients, r => r.IngredientId, i => i.Id, (r, i) => new { Relation = r, Ingredient = i } ) .Select(item => item) .ToList(); foreach (var ingredientNode in ingredientNodes) { this.Ingredients.Add(new RecipeIngredientViewModel(ingredientNode.Ingredient, ingredientNode.Relation)); } this._isInMenu = context.MenuItems.Where(item => item.RecipeId == this.Id).Any(); } this._isFullyLoaded = true; } }