I have been learning about T4 templates recently by looking at the awesome ADO.NET POCO entity generator. By using the POCO entity generator template as a base, I created a T4 template which generates metadata classes for a given Entity Data Model. This speeds coding by reducing the amount of typing required when creating view specific model and its metadata.
To use this template,
1) Download the template provided at the bottom.
2) Set two values in the template file. The first one should point to the EDM you wish to generate metadata for. The second is used to suffix the namespace and classes that get generated.
Once you add it, a number of classes will get added to your project based on the number of entities you have.
One of these classes is shown below. Note that the DisplayName, Required and StringLength attributes have been added by the t4 template.
The following shows how the generated class can be used from your project by creating a partial class with the entity name and setting the MetadataType attribute. Note that any changes to the auto generated files will be overwritten. You can also copy the code in the metadata class generated and create your own ViewModel class. In this case, you are responsible for keeping things in sync just like you would with any ViewModel class.
The template is super basic and does not take into account complex properties. I have tested it with the Northwind database. Feel free to modify the template to suite your requirements.
Use At Your Own Risk, Works on my machine running VS 2010 RTM/ASP.NET MVC 2
Mr. Incredible: Of course I have a secret identity. I don't know a single superhero who doesn't. Who wants the pressure of being super all the time?