Database Reference
In-Depth Information
Following is the output of the code in Listing 12-8:
Oops, tried to increase a salary too much!
Employees
Roger Smith makes $108,000.00/year
Jane Hall makes $81,500.00/year
How It Works
In the SaveChanges overridden event, we first retrieve all of the object state entries for the Employee entity that are in
the modified state. For each of them, we look for a modified “Salary” property with both original and current value,
which represents the value after modification. If they differ, we check to see if they differ by more than 10 percent. If
they do, then we throw an ApplicationException . Otherwise, we simply call the SaveChanges of the DbContext and
let Entity Framework save the changes to the database.
12-9. Retrieving the Original Association for Independent
Associations
Problem
You have an independent association. You want to retrieve the original association prior to saving the changes to the
database using ObjectContext.
Solution
Suppose that you have a model representing an order and the order's status (see Figure 12-12 ). The fulfillment of an
order goes through three stages, as represented in the OrderStatus entity. First the order is assembled. Next the order
is tested. Finally, the order is shipped. Your application has a business rule that confines all orders to this three-step
process. You want to enforce this rule by throwing an exception if an order goes, for example, from assembly to
shipped without first being tested. The association between Order and OrderStatus is an independent association.
Figure 12-12. A model with orders and their status
To solve this problem, we wire in a handler for the SavingChanges event. In this handler, we check to verify that
the order status changes follow the prescribed sequence. The code in Listing 12-9 provides the details.
 
 
Search WWH ::




Custom Search