Databases Reference
In-Depth Information
{
abstract class Metadata
{
[DataType(DataType.Date)]
public object SubmittedDate { get; set; }
}
}
For DataType.Date , the DataTypeAttribute provides a default format string {0:d} , which
with English (United States) regional settings is equivalent to the custom format string
{0:M/d/yyyy} . If you can live with a four-digit year format for the SubmittedDate, using
the DataTypeAttribute instead of the DisplayFormatAttribute is better. It is not only
simpler, but also keeps the presentation logic out of the entity model and keeps the meta-
data at the (higher) business level. Otherwise, using the DisplayFormatAttribute might
still be preferrable to embedding this information in multiple custom pages throughout
the application.
Enum Properties and Enumeration Templates
In entity design, you often come across the need to have a property with a fixed list of
possible values. A common way to present such properties to users is a drop-down list or a
group of radio-buttons. Discovering a need for such a control is another opportunity for
you to stop and carefully consider how this information should be presented in the entity
model.
If the list of possible values for a property will grow over time and the application will not
need any special logic for each new value, it is best to make this property a foreign key
and store the values in a separate table/entity. Dynamic Data automatically uses the
ForeignKey field templates for foreign key properties, and the users see a drop-down list
populated with values from the lookup table.
On the other hand, if the list of possible values for a property is fixed and especially if the
application will have different business rules for different values, it is best to define the list
of values as an enumerated type, such as the EmployeeType enum shown in the code in
this example:
public enum EmployeeType: byte
{
Fulltime = 0,
PartTime = 1,
Temporary = 2
}
Because the Entity Framework does not support enumerated types in .NET version 4, you
also have to rely on the EnumDataTypeAttribute to associate the enum type with an inte-
gral entity property as shown here:
 
 
Search WWH ::




Custom Search