Database Reference
In-Depth Information
throw new ApplicationException(
"Can't transition to that order status!");
}
}
}
}
}
Following is the output of the code in Listing 12-9:
Oops...better test first.
Order 2 [HAL 9000 Supercomputer], status = Ship
How It Works
We wired in a handler for the SavingChanges event. In this handler, we picked out the previous order status and the
new (current) order status and verified that the new status ID is one greater than the previous ID. Of course, the code
in Listing 12-9 doesn't look quite that simple. Here's how to find both the original order status and the new one.
For independent associations, in the object state manager there is an entry for the order, the order status, and a
relationship entry with one end pointing to the order and the other end pointing to the order status. The relationship
entry is identified by IsRelationship set to true .
First we get all of the orders tracked in the object context. To do this, we use the object state manager to get all of
the entries that are either modified or unchanged. We use a Where clause to filter this down to just entities of type Order.
For each order, we get all object state entries that are deleted. Then we use a Where clause to pick out just the
relationship entries (IsRelationship is true ) in the OrderStatus relationship set. Because there should be at most one
of these for any order, we pick the first. We look for the deleted relationships because when a relationship is changed,
the original one is marked deleted and the new one is created. Because we're interested in the previous relationship,
we look for a deleted relationship between the order and the order status.
Once we have the deleted relationship, we need to look at the original values for the entry to find both the order
end and the order status end. Be careful not to reference the current values here. Because the relationship is deleted,
referencing the current values will cause an exception. As we don't know which end of the relationship is the order
and which end is the order status, we test both.
With the original order status entity in hand, we simply check whether the original OrderStatusId is one less than
the new OrderStatusId. We created the OrderStatus objects so that their IDs would increment by one just to make the
code a little easier.
12-10. Retrieving XML
Problem
You want to treat a scalar property of type string as XML data.
Solution
Let's say that you have an XML column in a table in your database. When you import this table into a model, Entity
Framework interprets the data type as a string rather than XML (see Figure 12-13 ). The current version of Entity
Framework does not expose XML data types from the database. You want to work with this property as if it were an
XML data type.
 
 
Search WWH ::




Custom Search