Running EF T4 Code Generation Templates from Command Line

Posted on May 15, 2013. Filed under: Entity Framework, Visual Studio | Tags: , , , , , |

Since EF4 and Visual Studio 2010 you have been able to use T4 templates to generate code from models created using the EF Designer. Starting with EF5 and Visual Studio 2012 T4 based code generation is the default for new models created using the EF Designer.

When you create a new model using VS2012 or later you get two T4 templates (.tt files) nested under you EDMX file – one to generate the context and another to generate the entity classes. If you have opted in to T4 based code generation in VS2010 then you’ll have the same setup but the T4 templates won’t be nested under the EDMX. Expanding the T4 template will show the files that is is generating.


Whenever you save changes to your model in the EF Designer, the templates will re-run and update the generated code. But what if you want to run code generation outside of Visual Studio?


The answer is the TextTransform.exe command line tool. Here is the format to generate code based on one of the EF templates:

> "%ProgramFiles(x86)%\Common Files\Microsoft Shared\TextTemplating\11.0\TextTransform.exe"
<%%KEEPWHITESPACE%%>    -out &lt;PrimaryOutputCodeFile&gt;
<%%KEEPWHITESPACE%%>    -I "%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes"
<%%KEEPWHITESPACE%%>    -a !!NamespaceHint!&lt;NamespaceForGeneratedCode&gt;
<%%KEEPWHITESPACE%%>    &lt;T4TemplateToProcess&gt;

Note the three place holders you will need to supply:

  • PrimaryOutputCodeFile – The name of the file that the template should write code to. EF always uses the template file name replacing .tt with the extension for the language (.cs for C# or .vb for VB.NET). You’ll notice the entity template will also generate additional code files – one for each entity.
  • NamespaceForGeneratedCode – The namespace to use for generated classes.
  • T4TemplateToProcess – The template that you want to generate code from.

Extra Parameter for VS2012 Update 1 (or later)

If you are using Visual Studio 2012 Update 1 or later you also need to include an additional parameter to handle some changes we made to reduce the virtual memory usage during code generation:

-dp T4VSHost!Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor!"%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll"


Here is an example command to generate the code for the entities in my project shown in the screenshot earlier in this post:
Note: My command line is in the root folder of my project so the primary output code file and the template to process are specified with relative paths

&gt; "%ProgramFiles(x86)%\Common Files\Microsoft Shared\TextTemplating\11.0\TextTransform.exe"
<%%KEEPWHITESPACE%%>    -out BloggingModel.cs
<%%KEEPWHITESPACE%%>    -I "%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes"
<%%KEEPWHITESPACE%%>    -a !!NamespaceHint!Blogging
<%%KEEPWHITESPACE%%>    -dp T4VSHost!Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor!"%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll"


    Rowan works as a Program Manager for the ADO.NET Entity Framework team at Microsoft. He speaks at technical conferences and blogs at Rowan lives in Seattle, Washington with his wife Athalie. Prior to moving to the US he resided in the small state of Tasmania in Australia. Outside of technology Rowan's passions include snowboarding, mountain biking, horse riding, rock climbing and pretty much anything else that involves being active. The primary focus of his life, however, is to follow Jesus.


    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS


Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: