public bool AddCustomer(Customer customer)
{
using (_context = new TT_OnlineEntities())
{
_context.Customers.ApplyChanges(customer);
_context.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
ValidateCustomer(customer);
return _context.SaveChanges() > 0;
}
}
//here is the Validating method as well:
private void ValidateCustomer(Customer customer)
{
ValidationRule(customer.ChangeTracker.State != ObjectState.Deleted, "Deleting a Customer is not allowed.");
ValidationRule(customer.ChangeTracker.State != ObjectState.Added, "Adding a Customer is not allowed.");
ValidationRule(customer.ChangeTracker.State == ObjectState.Unchanged || !String.IsNullOrEmpty(customer.CustomerName), "You must submit a name for the Customer.");
ValidationRule(customer.ChangeTracker.State == ObjectState.Unchanged || !String.IsNullOrEmpty(customer.ModifiedDate.ToString()), "You must submit last modified date for the Customer.");
}
|