How to: Decorate Domain Properties With DataAnnotation Attributes
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
Telerik Data Access allows you to enhance the metadata about the domain classes and their members by decorating them with the following DataAnnotation attributes:
- KeyAttribute - marks the property/properties that uniquely identifies the objects of an entity
- RequiredAttribute - specifies that the property/properties needs to be explicitly set
- StringLenghtAttribute - specifies the maximum number of characters a string property/properties can contain
- DataTypeAttribute - associates the data field with an additional type. In Telerik Data Access the domain properties will be decorated with this attribute only in Database - First Scenario
Telerik Data Access can decorate the properties of your domain classes with these attributes during either the creation of the model or on a later stage when you modify it.
This topic will demonstrate to you how to control the enhacement of the domain properties with DataAnnotation attributes. The necessary settings can be found on the Code Generation Settings page in either the Advanced Options dialogue of the Create Model wizard or the Code Generation Settings tab in the Model Settings dialogue.
The process is as follows:
- Navigate to the Advanced Options dialogue of the Create Model wizard or open the Code Generation Settings tab in Model Settings.
In the Code Generation Options section check the Generate DataAnnotation Attributes check-box
The Generate DataAnnotation Attributes option is available only in Visual Studio 2010 and Visual Studio 2012. Additionally, the support for DataAnnotation attributes is introduced in .NET Framework 4.0.
Click Finish if you are using the wizard or OK for Model Settings
- Save the domain model if you are using the Model Settings dialogue
In both cases the domain properties of your classes will be decorated appropriately with the attributes:
If the domain properties of the model were created with DataAnnotation attributes and at some point you uncheck Generate DataAnnotation Attributes, the attributes will be removed once you save the model. You can restored them applying the described process.