How to: Execute Business Logic When the Object State Changes
This topic shows how to execute business logic when an entity changes state within the object context. The following example shows how to handle the ObjectStateManagerChanged event, which occurs when entities leave the context through delete or detach methods or enter the context through queries or add and attach methods.
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).
The following example demonstrates how to register for the
ObjectStateManagerChanged event. This event occurs when an object enters or leaves the context. In this example, an anonymous method is passed to the delegate. Alternatively, you can define the event handling method and then pass its name to the delegate. The anonymous method displays the status of the object whenever the event is triggered.