Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
    /// <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();
    }
Ejemplo n.º 3
0
    /// <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();
    }
Ejemplo n.º 4
0
    /// <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();
    }
Ejemplo n.º 5
0
    /// <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);
    }