public async Task <IActionResult> Create([Bind("ID,RecipeID,IngredientID,Quantity")] RecipeIngredient recipeIngredient) { if (ModelState.IsValid) { _context.Add(recipeIngredient); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["IngredientID"] = new SelectList(_context.Ingredient, "ID", "ID", recipeIngredient.IngredientID); ViewData["RecipeID"] = new SelectList(_context.Recipe, "ID", "ID", recipeIngredient.RecipeID); return(View(recipeIngredient)); }
/// <summary> /// Update existing entity using object as a new state. /// </summary> /// <param name="entity">New state of existing entity.</param> /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns> public async Task UpdateAsync(T entity) { await using CookingContext context = ContextFactory.Create(); T existing = await GetFullGraph(context.Set <T>()).FirstAsync(x => x.ID == entity.ID); Mapper.Map(entity, existing); await context.SaveChangesAsync(); }
/// <summary> /// Update existing entity using object as a new state. /// </summary> /// <typeparam name="TProjection">Projection containing part of new state for entity.</typeparam> /// <param name="entity">New state of existing entity.</param> /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns> public async Task UpdateAsync <TProjection>(TProjection entity) where TProjection : Entity { await using CookingContext context = ContextFactory.Create(); T existing = Get(entity.ID, context, isTracking: true); Mapper.Map(entity, existing); await context.SaveChangesAsync(); }
/// <summary> /// Remove entity from database. /// </summary> /// <param name="id">Id of entity to delete.</param> /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns> public virtual async Task DeleteAsync(Guid id) { using CookingContext context = ContextFactory.Create(); // Remove entity without loading it, using stub object. context.Set <T>().Remove(new T { ID = id }); await context.SaveChangesAsync(); }
/// <summary> /// Create new entity in database. /// </summary> /// <param name="entity">Entity to insert into database.</param> /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> public async Task <Guid> CreateAsync(T entity) { await using CookingContext context = ContextFactory.Create(); entity.Culture = GetCurrentCulture(); await context.Set <T>().AddAsync(entity); await context.SaveChangesAsync(); return(entity.ID); }