Geeks With Blogs

News

This blog has moved to ericnelson.wordpress.com

 Subscribe in a reader

Add to Google Reader or Homepage


Links
View my teams slideshare
These postings are provided "AS IS" with no warranties, and confer no rights.



IUpdateable has moved to ericnelson.wordpress.com Please visit http://ericnelson.wordpress.com

[This is the second in a series of posts on getting started with the new features in Entity Framework 4 based on the demos I did in my session at TechEd Europe in Berlin last week (Nov 2009).]

Code generation from the Entity Data Model (EDM) has changed significantly in version 4 of the Entity Framework. The product does retain a backwardly compatible model of code generation but also now includes T4 templates for code generation. This is a fantastic addition to the Entity Framework as many developers want to (easily) control the code generated from the EDM to meet their specific needs, which was very difficult to do in version 1 as it depended on the CodeDom. T4 gives us:

  • Full control over the code generation from an EDM
  • The ability for developers to easily customise the templates produced by Microsoft
  • The ability for Microsoft to easily add additional templates in the future
  • The ability for developers to easily share templates between projects, teams and companies

T4 was first introduced in Visual Studio 2008 and stands for Text Template Transformation Toolkit – a code generation tool similar in capabilities to the likes of CodeSmith.

T4 was largely overlooked in Visual Studio 2008, in the main because there was no real UI for T4. In general  I come across very very few developers who have heard of or understand the power of T4. However, now that the Entity Framework team (and the ASP.NET MVC team) have adopted T4 wholeheartedly I expect T4 will itself get the attention it deserves.

The EF team include a single T4 template in Beta 2 but add a further template in CTP2 for self tracking entities  (In Beta 1 they also had a singel template but added an additional two in the companion CTP1).

By default you are not using T4 if you add a new EDM or if you open a version 1 EDM. You need to enable T4 code gen for your EDM. You do this by right clicking on the EDM design surface and then add in the template(s).

In Beta 1 this was called (confusingly) Add New Artifact Generation Item:

image 

In Beta 2 it is renamed to Add Code Generation Item:

image

This will display the available choices:

Beta 1 with CTP 1

image

Beta 2 with CTP 2

image

Once you have added a template you will notice that the Custom Tool generation for the EDM is now blank (It normally is the value EntityModelCodeGenerator):

image

In my case I have added the POCO Code Generator (which was available in CTP1 for Beta 1 and will reappear in a future CTP for Beta 2). My solution now contains two T4 files – one to generate the Object Context class and another responsible for the generation of a class (and C# file) per Entity in the EDM. Note that a single T4 template can generate several files:

image

If we take a brief look at the contents of SimpleModel.Context.tt:

image

We can see that the text wrapped inside <# #> is evaluated by T4 to ultimately generated the resulting C# files. The code outside is just text in the final output. This approach should feel very familiar to anyone who has developed web sites using classic ASP. For example, this section of SimpleModel.Types.tt:

image

generates the partial class code in the C# file Order.cs:

image

All pretty straightforward really – although a lot of “smarts” is contained within those .tt files! I will add a more general post on T4 shortly.

Posted on Thursday, November 19, 2009 1:47 PM Data , Entity Framework , EF4 | Back to top


Comments on this post: Getting Started with Entity Framework 4 - Templated Code Generation

# re: Getting Started with Entity Framework 4 - Templated Code Generation
Requesting Gravatar...
Hello Guy, nice blog. Do you know when will be released the POCO generator for EF4 beta2? I read your post but as you said I couldn't find this template.
Left by Higor on Dec 18, 2009 5:49 PM

Your comment:
 (will show your gravatar)


Copyright © Eric Nelson | Powered by: GeeksWithBlogs.net | Join free