예제 #1
0
        public Order_DataAccessLayer.Model.Customer GetCustomerByID(int?id)
        {
            try
            {
                Order_DataAccessLayer.Model.Customer entity = _context.Customers.FirstOrDefault(x => x.CustomerId == id);
                if (entity != null)
                {
                    _context.Customers.Find(id);
                }
                else
                {
                    throw new Exception("Customer not found");
                }
                return(entity);
            }

            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }
예제 #2
0
 public void AddCustomer(Order_DataAccessLayer.Model.Customer customer)
 {
     try
     {
         _context.Customers.Add(customer);
         _context.SaveChanges();
     }
     catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
     {
         Exception raise = dbEx;
         foreach (var validationErrors in dbEx.EntityValidationErrors)
         {
             foreach (var validationError in validationErrors.ValidationErrors)
             {
                 string message = string.Format("{0}:{1}",
                                                validationErrors.Entry.Entity.ToString(),
                                                validationError.ErrorMessage);
                 raise = new InvalidOperationException(message, raise);
             }
         }
         throw raise;
     }
 }
예제 #3
0
 public void UpdateCustomer(Order_DataAccessLayer.Model.Customer customer)
 {
     try
     {
         _context.Entry(customer).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
         Commit();
     }
     catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
     {
         Exception raise = dbEx;
         foreach (var validationErrors in dbEx.EntityValidationErrors)
         {
             foreach (var validationError in validationErrors.ValidationErrors)
             {
                 string message = string.Format("{0}:{1}",
                                                validationErrors.Entry.Entity.ToString(),
                                                validationError.ErrorMessage);
                 raise = new InvalidOperationException(message, raise);
             }
         }
         throw raise;
     }
 }