public async Task <T> AddAsync(T entity) { _dbContext.Set <T>().Add(entity); await _dbContext.SaveChangesAsync(); return(entity); }
public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductId) { _logger.LogError("Product Not Found, Bad request"); return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { _logger.LogInformation("Product Not Found"); return(NotFound()); } else { throw; } } _logger.LogInformation("Not Content"); return(NoContent()); }
public async Task <IHttpActionResult> PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductID) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <Customer> AddCustomer(Customer customer) { await _adventureWorksDbContext.Customers.AddAsync(customer); await _adventureWorksDbContext.SaveChangesAsync(); return(customer); }