コード例 #1
0
 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.");
     }
 }
コード例 #2
0
 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.");
     }
 }
コード例 #3
0
ファイル: Edit.cshtml.cs プロジェクト: asge4900/Northwind
        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"));
        }
コード例 #4
0
 /// <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);
     }
 }
コード例 #5
0
 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));
     }
 }
コード例 #6
0
 public void UpdateCategory(Categories category)
 {
     dbContext.Attach(category);
     dbContext.Entry(category).State = EntityState.Modified;
 }
コード例 #7
0
 public void UpdateProduct(Products product)
 {
     dbContext.Attach(product);
     dbContext.Entry(product).State = EntityState.Modified;
 }
コード例 #8
0
ファイル: EfRepository.cs プロジェクト: piranha-code/rl
 public virtual void Update(TEntity entity)
 {
     _dbContext.Attach(entity);
     _dbContext.Entry(entity).State = EntityState.Modified;
 }