Generate Output from T4 at Build Time

Text templates (aka T4) is a great feature in Visual Studio, that proves helpful in many scenarios. I use it for instance in developing Alchemy. However, it has a significant drawback: it does not generate code automatically when you build your solution. To build the files and generate their output you have to manually run either Run Custom Tool command for each .tt file, or Transform All Templates for the entire solution.

Run Custom Tool
Transform All Templates

The good news is that Visual Studio 2010 has added capabilities for building the text templates files automatically at the build time. Basically, what you have to do is two things: first install the Visual Studio Visualization and Modeling SDK. Second, manually add the following to the project file:

And that should do the trick. When you build the project, the .tt files will also be built.

To read more about this topic see:

5 Replies to “Generate Output from T4 at Build Time”

  1. Hi,

    that actually works fine, however in one of my templates I have a include of “EF.Utility.CS.ttinclude” since the template deals with EF entities. Now when I execute the template with “Run Custom Tool” command it obviously works fine, during build however it fails, telling the EF.Utility…. include cannot be resolved.

    I guess the problem is that the transformation runs in a different location and hence the file cannot be found. Do you have any hints on how this could be solved??

  2. Any idea why I get this error when the transform starts ?
    C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets(396,5): error : There was a problem loading the assembly ‘C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll’

  3. Hello all

    I solved the problem of Juri, putting this information in the file project

    true
    false
    true
    $(DevEnvDir)\Extensions\Microsoft\Entity Framework Tools\Templates\Includes

    but i have a problem compiling in visual studio that says
    Could not obtain DTE from host

    i tried to use a pre-build event, but only works in command line not in compilation mode

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.