Database Reference
In-Depth Information
modification of the object. There is also a comments column that is no longer used and should always be set to “N/A”.
Because we no longer use the comments, we don't have this property available on the entity. You want to initialize the
PurchaseOrderId column, the date fields, the Paid column, and the comments column to default values. Our model is
shown in Figure 12-10 .
Figure 12-9. The PurchaseOrder table with several columns that need default values
Figure 12-10. The model created from the PurchaseOrder table in Figure 12-9
We will illustrate three different ways to set default values. Default values that don't need to be dynamically
calculated can be set as the Default Value for the property in the conceptual model. Select the Paid property and view
its Properties. Set the Default Value to false .
For properties that need to be calculated at runtime, we need to override the SaveChanges event. This is
illustrated in Listing 12-7. In this event, if the object is in the Added state, we set the PurchaseOrderId to a new GUID
and set the CreateDate and ModifiedDate fields.
To illustrate setting the default value outside of the conceptual model, we can modify the store layer to set a
default value for the comments column. This approach would be useful if we didn't want to surface some properties
in the model, yet wanted to set their default values. To set the default value through the store layer, right-click the
.edmx file and select Open With XML Editor. Add DefaultValue="N/A" to the <Property> tag for the Comment
property in the SSDL section of the .edmx file.
 
Search WWH ::




Custom Search