Sneak Peek – Using Code Generation Templates with the Entity Framework 4.0

Published 19 May 09 01:42 PM | dpblogs 

In the first version of the Entity Framework code generation was implemented internally using CodeDom. The Entity Framework APIs provided some hooks for customizing the generation, but customizing was tricky, relatively inflexible and wasn’t integrated into Visual Studio.

In .NET 4.0 all these limitations go away.

The Entity Framework now leverages T4, Text Template Transformation Toolkit, which makes customizing Code Generation easy, flexible and powerful, and the experience is also fully integrated into Visual Studio.

T4 itself is very simple, in fact you can try it out in Visual Studio 2008 today if you want. T4 provides a clean way of interleaving control logic and references to variables with output text, that is reminiscent of the techniques used widely in web server programming (for those familiar with old-school ASP or ASP.NET MVC views it will look vaguely familiar).

Out of the box the Entity Framework will ship with two T4 templates, both of which will produce EntityObject derived Entities. One in VB that produces VB code, and one in C# that produces, yes you guessed it, C# code.

So how does it all work?

Well if you are happy with our default code-gen, you don’t do anything, and your generated code lives under the EDMX file as before. i.e:

If however you want to customize the code, you add a new turn T4 template into your VS project, by right clicking on the EDMX designer canvas, and selecting “Add New Artifact Generation Item…” from the context menu.
This will show a screen where you can select one of the registered templates:

 

Once you’ve added one to your project the generated code no longer resides under the [model].edmx file it will reside under the [model].tt file instead:

 

Now to customize you Entities you simply modify the tt template file. Every time you save changes to the template, the dependent code is regenerated automatically.

It is that simple.

Overtime we plan to ship more templates, for example to produce POCO Entities or Self Tracking Entities.

Stay tuned for a more in depth discussion of our Code Generation enhancements soon.

In the meantime check out the MSDN documentation for more information.

Cheers,

Alex James
Program Manager, Entity Framework

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Sneak Peek ??? Using Code Generation Templates with the Entity Framework 4.0 | ASP NET Hosting said on May 19, 2009 5:17 PM:

PingBack from http://asp-net-hosting.simplynetdev.com/sneak-peek-%e2%80%93-using-code-generation-templates-with-the-entity-framework-40/

# progg.ru said on May 20, 2009 1:28 AM:

Thank you for submitting this cool story - Trackback from progg.ru

# Matthieu MEZIL said on May 20, 2009 11:42 AM:

Hier, j’ai bloggé sur mon pattern de génération T4. En discutant un petit peu, j’ai pu me rendre compte

# ADO.NET team blog said on May 21, 2009 8:46 PM:

Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

# Thanigainathan said on May 24, 2009 10:15 AM:

Is this feature("T4 Templates ") is a general one and can be made available for other project items too ?

# Web开发技术 said on May 30, 2009 10:13 PM:

Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

# Web开发技术 said on May 30, 2009 11:50 PM:

【译者按】 Entity Framework 1.0 发布也有一段时间了,但感觉用的人很少。其中一个很大的原因,也许就是不支持POCO。要知道,Entity Framework 1.0的做法是让你的实体从EF的基类继承而来

# Noam Ben-Ami said on June 12, 2009 4:11 PM:

Yes, Thanigainathan: T4 Template support is built into Visual Studio and can be used by anyone for any generation task. This technology is a core part of the DSL Toolkit.

# radyo dinle said on July 19, 2009 10:40 AM:

Hier, j’ai bloggé sur mon pattern de génération T4. En discutant un petit peu, j’ai pu me rendre compte

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker