public void Update(Product product) { if (IsValid(product)) { _logger.LogInformation("Updating record in database"); try { _dbContext.Attach(product).State = EntityState.Modified; _dbContext.SaveChanges(); _logger.LogInformation("Record updated successfully"); } catch (DbUpdateConcurrencyException) { _logger.LogError($"A concurrency violation is encountered while saving product '{product.ProductID}' to the database."); } catch (DbUpdateException) { _logger.LogError($"An error is encountered while saving product '{product.ProductID}' to the database."); } } else { _logger.LogError($"Error updating product '{product.ProductID}' in database. Model is invalid."); } }
public void Update(Category category) { _logger.LogInformation("Updating category in database"); try { if (IsValid(category)) { _dbContext.Attach(category).State = EntityState.Modified; _dbContext.SaveChanges(); _logger.LogInformation("Category updated successfully"); } else { _logger.LogError("Error updating category in database. Model is invalid."); } } catch (DbUpdateConcurrencyException) { _logger.LogError($"A concurrency violation is encountered while saving category '{category.CategoryID}' to the database."); } catch (DbUpdateException) { _logger.LogError($"An error is encountered while saving category '{category.CategoryID}' to the database."); } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Employee).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(Employee.EmployeeID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
/// <summary> /// Update a single employee /// </summary> /// <param name="employee">Valid employee</param> /// <returns></returns> public static bool Update(Employees employee) { using (var context = new NorthwindContext()) { context.Attach(employee).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public async Task <ActionResult> RemoveCustomer(string CustomerId) { try { Customer customer = new Customer() { CustomerID = CustomerId }; _context.Attach(customer); _context.Customers.Remove(customer); _context.SaveChanges(); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void UpdateCategory(Categories category) { dbContext.Attach(category); dbContext.Entry(category).State = EntityState.Modified; }
public void UpdateProduct(Products product) { dbContext.Attach(product); dbContext.Entry(product).State = EntityState.Modified; }
public virtual void Update(TEntity entity) { _dbContext.Attach(entity); _dbContext.Entry(entity).State = EntityState.Modified; }