Combining Entity Framework Self-Tracking, Extensible Metadata, and Data Annotations
A few months ago, I read this great post about the Portable Extensible Metadata (PEM) of Entity Framework 4. The PEM relies on the extensibility of the entity framework EDM, which enables you to add your own XML content into the EDM. Once placed in the EDM, you can write code that investigates the MetadataWorkspace and do operations according to what you’ve written in the EDM.
One usage for example which is demonstrated in the PEM extension is adding validations to entities and properties, and placing this validation information inside the EDM for later use. To let the developer define the validation rules, the PEM demonstrates another technique – extending the entity framework designer. The entity framework designer is extensible in a way that you can add new properties in the properties window to configure entity types, properties, associations and more; you can even decide if the properties will be applied for the CSDL or the SSDL part of the EDMX. If you want to write your own extensions for the designer, take a look at the designer starter kit.
One thing that people noted about the PEM extension which also bothered me – the extension includes a new T4 template that creates C# validation code according to the validation rules it finds in the EDMX, but this code is simply “hard-coded” IF statements; it would be a great if the T4 could instead use the validation rules to apply data annotations to the generated entity classes, just like you can do with EF Code-Only.
So … I sat down and wrote a template for that purpose. I admit, I didn’t invest too much time in this, and didn’t cover all the validation types, but if you have use for this template, I’m sure you can add the missing pieces (if you do, I’d be happy if you can send me your updated template).
The template itself is based on the Self-Tracking template to which I’ve added some code to generate data annotation attributes for every primitive property.
Once your entities have data annotations, you can use them for example in ASP.NET MVC or Silverlight, and gain the automatic validation mechanism. You can also write another T4 to create validation methods that manually calls the System.ComponentModel.DataAnnotations.Validator class so you can validate you entities on the server side, for example before saving them to the DB (I might publish another post in the future with a sample template that does this).
To use the template, download the SelfTrackingWithDataAnnotationsCSharp.zip file and place it in the item templates folder under your documents folder (I recommend you reopen VS after you copy the file):
%userprofile%\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#
Once you place the zip file in the folder (do not extract it, place it as it is), you’ll be able to add a new item called “ADO.NET Self-Tracking Entity Generator + Data Annotations” to your project (sorry for the length).
You can also add this file by right-clicking in an opened EDMX file and selecting “Add Code Generation Item…”.
Feel free to edit the template and improve it for your needs.