[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:
In Beta 2 it is renamed to Add Code Generation Item:
This will display the available choices:
Beta 1 with CTP 1
Beta 2 with CTP 2
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):
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:
If we take a brief look at the contents of SimpleModel.Context.tt:
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:
generates the partial class code in the C# file Order.cs:
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.