The following code library applies only to versions prior to Q2 2013. As of Q2 2013 Telerik OpenAccess ORM supports generation of DataAnnotation attributes out of the box.
OpenAccess version
|
Q1 2012 SP1 (2012.1.301.2)
|
.NET version |
4.0 |
Visual Studio version |
2010
|
Programming language |
C#, VB.Net
|
PROJECT DESCRIPTION
This project demonstrates how to generate data annotation for your domain types based on the domain model schema.
Data annotations are generated using custom text template that should be included in the domain model project.
This domain model is based on SofiaCarRental21 sample database.
SofiaCarRental21 database can be created using the SofiaCarRental21.sql script.
Data annotation attributes that are supported by this custom code generation template are:
-
KeyAttribute
-
RequiredAttribute
-
StringLenghtAttribute
In order to achieve the desired result please:
1.
Create a domain model
2.
Add the DataAnnotations.tt template to the project. Set its Custom Tool to "TextTemplatingFileGenerator" if not set.
3.
Edit the template file:
3.1
Please change line 11:
For Visual Studio 2010 it should be:
and for Visual Studio 2012:
You should replace the value of the assembly name with the full path to Telerik.OpenAccess.Common.UI assembly on YOUR machine.
3.2
Please change line 13:
You should replace the value of the assembly name with the full path to Telerik.OpenAccess assembly on YOUR machine.
3.3
OPTIONAL: Edit the name of the .rlinq file on line 62:
By default the template will search the directory where it resides for .rlinq files. If it finds one it will use it.
You have to change the rlinq file path if:
-
there are more than one domain models in your project
-
the rlinq file discovery routine fails to find your rlinq file for some reason
4.
Transform the DataAnnotations.tt template either by saving it or by invoking the Transform All Templates command in Visual Studio.
5.
Enjoy your auto-generated data annotations.
If you make any changes in the domain model that affect the data annotation (like addition of new column or change of the allowed string length) you should consider regenerating the data annotation code by invoking Transform All Templates.
Keep in mind that changes made in the generated code file could easily get lost if the file is regenerated!
If you need one-time code generation please delete/exclude from project the DataAnnotations.tt in order to prevent any code loss in the generated file.
If you want to add additional data annotation attributes you can do it in the generated file, but make sure that the text template won't overwrite your changes.