How to: Execute Business Logic During Association Changes
This topic shows how to execute business logic when an association between entities changes.
The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).
You must also add the following using statement (Imports in Visual Basic) to your code:
Example
This example extends the example in the How to: Use an EntityReference to Change Relationships Between Objects topic. This example shows how to check the order status when the shipping address changes by handling the AssociationChanged event on the EntityReference for the Address object that represents the shipping address. If the order status is greater than 3, the order cannot be changed and an exception is raised. The delegate is defined in the constructor for the SalesOrderHeader partial class, and the handler for this event is also implemented in this partial class. This ensures that the order status is checked whenever the shipping address is changed for an order.
You can also call the OnPropertyChanging and OnPropertyChanged methods in this event to raise the PropertyChanging and PropertyChanged events, respectively. These events notify client controls of the association changes.
To validate changes at the other end of the SalesOrderHeader-Address relationship, a similar technique could be used to register the AssociationChanged event on the EntityCollection of SalesOrderHeader objects that are related to a shipping address.