Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the Entity Framework t4 templates for Data Annotations?

I have been googling this non stop for 2 days now and can't find a single complete, ready to use, fully implemented t4 template that generates DataAnnotations. Do they even exist?

I generate POCOs with the standard t4 templates. The actual database table has metadata that describes some of the validation rules, eg not null, nvarchar(25), etc.

So all I want is a t4 template that can take my table and generate a POCO with DataAnnotations, eg

public class Person
{

[Required]
[StringLength(255)]
public FirstName {get;set}

}

It is a basic and fundamental requirement, surely I can not be the first person in the entire world to have this requirement? I don't want to re-invent the wheel here. Yet I haven't found it after searching high and low for days.

This must be possible (and hopefully must be available somewhere to just download) - it would be criminally wrong to have to manually type in these annotations when the metadata for them already exists in the database.

like image 688
JK. Avatar asked May 01 '10 02:05

JK.


People also ask

What are T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

What is the purpose of using T4 templates?

Design-time T4 text templates let you generate program code and other files in your Visual Studio project.

What is data annotation in Entity Framework?

DataAnnotations is used to configure the classes which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC which allows these applications to leverage the same annotations for client-side validations.


1 Answers

Here is what you want!

T4 Metadata and Data Annotations Template

This T4 template handles generating metadata classes from an Entity Framework 4 model and decorates properties with data annotation attributes such as [Required] and [StringLength]. The [DataType] attribute is also applied when appropriate. It'll also generate ErrorMessage values based upon property names for required fields.

like image 168
Jalal Avatar answered Sep 28 '22 04:09

Jalal