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.

SolutionWithTemplates

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?

 


TextTransform.exe

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"
    -out <PrimaryOutputCodeFile>
    -I "%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes"
    -a !!NamespaceHint!<NamespaceForGeneratedCode>
    <T4TemplateToProcess>

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"

Example

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

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

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

3 Responses to “Running EF T4 Code Generation Templates from Command Line”

RSS Feed for RoMiller.com Comments RSS Feed

[…] Miller covered Running EF T4 Code Generation Templates from Command Line and how to deal with the following error introduced in VS2012 Update 1 – Processor named […]

When I try this the T4 is transformed successfully, the ouput files are generated, however, they are not added to the .csproj file. Transforming via the devenv button adds all generated files to the project. Is there any way to make the command line utility act the same way? Or perhaps to add these files to the .csproj via an additional command line?

i found a workaround which should work if your T4s are based on the EF include file: http://stackoverflow.com/a/18971000/38368


Where's The Comment Form?

    About

    Rowan works as a Program Manager for the ADO.NET Entity Framework team at Microsoft. He speaks at technical conferences and blogs at romiller.com. 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.

    RSS

    Subscribe Via RSS

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

    Meta

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

Follow

Get every new post delivered to your Inbox.

Join 163 other followers

%d bloggers like this: