Running EF T4 Code Generation Templates from Command Line

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"