ASP.NET Dynamic Data Attributes
Yesterday a new release of the ASP.NET Dynamic Data Preview was posted on Code Gallery.
Dynamic Data uses a data model to interact with the underlying database. From the data model, Dynamic Data reads the metadata information to perform some built-in operations, such as, type validations or a required field validation. But you can extend the data model and add some extra metadata to a table or a particular field by using attributes.
These attributes are in two namespaces: System.ComponentModel and System.ComponentModel.DataAnnotations.
Here is the list of attributes you can use:
Attribute name | Description | Category |
DisplayNameAttribute | Specifies the header name for a column in the database. | Display |
DescriptionAttribute | Provides a textual message that will popup when the mouse pointer is moved over a field during editing (tooltip). | |
DefaultValueAttribute | Provides a default value for a field when inserting a new value. | |
DataTypeAttribute | Adds additional information about a data field, that is commonly treated as a plain string. Options include:
|
|
DisplayColumnAttribute | Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds. | |
DisplayFormatAttribute | Specifies a DataFormatString that can be used to format how numbers and dates are displayed. | Display |
MetadataTypeAttribute | Specifies the class that provides metadata for an entity class. | |
RangeAttribute | Provides range based validation where the value of a field must be between a min and max value. Built-in field controls will render this using a RangeValidator control. | Validation |
RegularExpressionAttribute | Provides regular expression based validation where the value of a field must match a regular expression pattern. Built-in field controls will render the using a RegularExpressionValidator control. | Validation |
RequiredAttribute | Specifies that the field is required. Built-in field controls will render this using a RequiredFieldValidator control. | Validation |
ScaffoldColumnAttribute | Specifies if a data field is visible to the scaffolding mechanism. | Display |
ScaffoldTableAttribute | Specifies if a table is visible to the scaffolding mechanism. | Display |
StringLengthAttribute | Specifies the maximum length of a string. | Validation |
UIHintAttribute | Specifies a field template to use to render the particular field. | Display / Behavior |
To apply an attribute to a table or a particular field, you cannot change the class generated by the O/R Designer or you will end up losing the changes if you update the data model.
So the way you do this with Dynamic Data, is that you first create a partial class with name of your table. If the metadata refers to the table, you can apply the attribute in this partial class. For example:
[DisplayColumn("FirstName")]
public partial class Employee { }
But, when you want to add metadata to a particular field, you also need to create a special metadata class where you're going to apply the attribute to a particular field. And then, you're going to connect the partial entity class with this metadata class with the MetadataTypeAttribute, like in the following example:
[MetadataType(typeof(ProductMetaData))]
public partial class Product { }
public class ProductMetaData {
[DisplayFormat(DataFormatString = "{0:C}")]
public object UnitPrice { get; set; }
}
I'll keep posting on Dynamic Data!