Customizing EDM Code Gen in EF4

Previous "What's New in EF 4 Posts"ef4

In the current version of Entity Framework, classes within the System.Data.EntityDesign namespace are responsible for the default code generation which creates the ObjectContext and various EntityObject classes from your model. You can use the same classes in this namespace to write your own code generator.

In VS2010, this changes dramatically. T4 templates, (T4=Text Template Transformation Toolkit) which are still a little known Visual Studio feature introduced into VS2008, are being used for the entity class code generation.

If you want to customize how the entity classes and objectContext are created, you can start with the default T4 Template and then modify it.

T4 syntax looks a bit like something between .NET code and HTML. There is not a lot of support built into VS2008 for it, certainly no editor. But there are editors such as VisualT4 from Clarius to help you with your T4 editing.

I have never used T4 before so I will use this blog post to be a public guinea pig for customizing entity class generation the new way.

I'll start by creating a VERY simple model using the EDM Wizard.

tta 

By default, a class file is generated using the Custom Tool defined in the model's Properties window, "EntityModelCodeGenerator".

ttb

 

It took me a few minutes to figure out how to get started. My instinct was to look in the Solution Explorer. But the starting point is in the designer's context menu: "Add New Artifact Generation Item..." Not very obvious but that's the one.

ttc

When you choose this, you will then get an Add New Item dialog filtered on the ADO.NET EntityObject Generator item and a default name, in my case Model1.tt which I will leave.

ttd

Next comes a scary warning. Just click OK and feel free to check "do not show this message again."

tte

Notice that after creating this item, there are a few changes in Solution Explorer and properties.

ttf

The model class is now attached to the new Model1.tt file, not to the Model1.edmx file and the Custom Tool for the edmx file is empty (previously that was EntityModelCodeGenerator).

 

Now your ready to start customizing.

My goal will be a simple one. I will remove the DataContractAttribute from the EntityObject classes since I have no plans on using my model in a WCF service.

Here is the signature for my address entity created by the default generator. Note that my project is C# so the C# version of the template was added. THere is also a VB template! (Thanks guys!)

[EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="address")]
[Serializable()][DataContractAttribute(IsReference=true)]
public partial class Address : EntityObject{}

Open up the Model1.tt file.

Note that text which is inside <#   #> is code to be executed. Text which is not contained inside the angle brackets will simply be written out to the file.

Search for DataContract which you will find near the top. DataContract is part of a set of text that just gets written to the file. In this screenshot, the text highlighted in Pepto Bismol pink is code that will be excuted to generate the entity code, whereas the text that is not highlighted is text which is written to the generated code.

ttg

Delete the line with DataContract.

 tth

Save the template. The code generation will happen automatically. You do not need to force it to happen.

Now when I open up the class file I can see that the signature of both of the entity classes from my model have changed.

 [EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="address")]
 [Serializable()]
 public partial class Address : EntityObject{}
 [EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="Person")]
 [Serializable()]
 public partial class Person : EntityObject{}

That should get you started so that you can take control of how your model is transformed into classes to meet your domain needs.

Shout it
#1 Joe on 5.28.2009 at 11:01 AM

Nice, you might also want to use tangible T4 Editor for VS2010 which is available at visualstudiogallery.msdn.microsoft.com/.../050a2994-cd28-4 via Download button ;-) or from inside the Visual Studio Extension Manager.

#2 Julie on 5.28.2009 at 11:05 AM

Thanks, Joe. In fact, I wrote a whole blog post about that!

thedatafarm.com/.../get-vs2010-exte

#3 Morten on 6.04.2009 at 11:22 PM

Hi. I am using VS 2010 Beta 1, but my template window is completly empty, except from the one online template it can find (A wpf about box). Havent found a solution for this problem yet :/

#4 Julie on 6.05.2009 at 7:10 AM

Hi Morten,

The project/project item templates are a completely different thing than the T4 templates. And I don't happen to know how to advise you about the missing item templates. I'd recommend checking this .NET4/VS2010 Beta forum: social.msdn.microsoft.com/.../threads

hth

julie

#5 Marquez on 6.12.2009 at 5:17 PM

Hi, Julie.

I am a bit confused by the statement, given in this video regarding model-first development: http://channel9.msdn.com/shows/10-4/10-4-Episode-15-Model-First-Development-with-the-Entity-Framework-40/,

that the generated SQL may also be customized by using T4 templates. Is it me, or haven't Microsoft added this functionality yet?

Leave a Comment