Database Reference
In-Depth Information
How It Works
In the solution, we implement the partial methods OnUserNameChanging() and OnUserNameChanged() to monitor the
property change activity. The OnUserNameChanging() method is called when the property value is being set. Here we
have an opportunity to throw an exception or, as in our example, simply report that the UserName is being set to a
value of five characters or fewer.
The OnUserNameChanged() method is called after the property has been changed. Here we simply set the IsActive
property based on the length of the final UserName property value.
These partial methods are created by Entity Framework as part of the code generation process. The names of
the partial methods are derived from the property names. In our case, each method name included the name of the
property. These partial methods are called inside the setter for each property.
You may be wondering a bit about the output of code. Notice that the partial methods are called twice in our
example. They are called when the property value is set. They are also called when the User instances are materialized
from the database. This second call happens, of course, because the materialization process involves setting the
property value from the persisted value in the database.
In addition to these two partial methods, Entity Framework exposes two events for monitoring property changes.
These events, PropertyChanging and PropertyChanged , are raised when any property on an Entity is changed. The
sender of the event is the instance of the entity, and the PropertyEventArgs parameter contains a PropertyName that
holds the name of the property that is changing or that has changed. Because these events are fired for any property
change on the entity, they can be useful in some scenarios, particularly if you have an entity with many properties.
They are somewhat less useful in practical terms because they don't readily expose the current and proposed values
for the property.
When our UserName property value changes, the sequence is as follows:
1. OnUserNameChanging() method is called.
2. PropertyChanging event is raised.
3. PropertyChanged event is raised.
4. OnUserNameChanged() method is called.
The PropertyChanging and PropertyChanged events are not raised when a navigation property value is changed.
The state of an entity changes only when a scalar or complex property changes.
12-3. Logging Database Connections
Problem
You want to create a log entry each time a connection is opened or closed to the database.
Solution
Entity Framework exposes a StateChange event on the connection for a DbContext. To create a log entry each time a
connection is opened or closed, we need to handle this event.
Suppose our model looks like the one shown in Figure 12-3 . In Listing 12-3, we create a few instances of a
Donation and save them to the database. The code implements the override SaveChanges() method to wire in our
handler for the StateChange event.
 
Search WWH ::




Custom Search