Getting Started with Entity Framework 4 – Templated Code Generation


[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).

Read the rest of this post (with lots of screen grabs) over on my primary blog http://geekswithblogs.net/iupdateable

Skip to main content