Database Reference
In-Depth Information
Chapter 12
Customizing Entity Framework
Objects
The recipes in this chapter explore some of the customizations that can be applied to objects and to the processes in
Entity Framework. These recipes cover many of the “behind the scenes” things that you can do to make your code
more uniform by pushing concerns about things like business rule enforcement out of the details of your application
to a central, application-wide implementation.
We start off this chapter with a recipe that shows you how to have your own code executable anytime
SaveChanges() is called within your application. This recipe and a few others are particularly useful if you want to
enforce business rules from a single point in your application.
In other recipes, we show you how to track database connections, how to automate responses to collection
changes, how to implement cascading deletes, how to assign default values, and how to work with strongly typed
XML properties.
The common thread of all of these recipes is extending the objects and processes in Entity Framework to make
your code more resilient, uniform, and maintainable.
12-1. Executing Code When SaveChanges( ) Is Called
Problem
You want to execute code anytime SaveChanges() is called in a data context.
Solution
Let's say that you have a model that represents a job applicant. As part of the model, you want the file containing the
applicant's resume to be deleted when the applicant's record is deleted. You could find every place in your application
where you need to delete an applicant's record, but you want a more consistent and unified approach.
Your model looks like the one shown in Figure 12-1 .
 
Search WWH ::




Custom Search