Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 templates not generating output in new VS2017 csproj projects

I migrated a project.json/.xproj project to the newer CS2017 .csproj format.

The project contains a T4 (.tt) template file.

It doesn't regenerate its output on save or build. The output .cs file isn't nested below the .tt file either.

Is there something I have to do to get this working?

like image 907
Drew Noakes Avatar asked Mar 22 '17 22:03

Drew Noakes


People also ask

What are T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.


2 Answers

I realise this is 2+ years old but for those bumping into this issue years on like me, the method listed below works for me without installing anything. I had the exact same issue, after upgrading a project from Visual Studio 2010 to Visual Studio 2017. YMMV. Make a backup copy of your .csproj file before you start.

Forcing rebuild of all .tt files when you build your project can be achieved without installing anything, by editing the .csproj project file. Editing the .csproj file seems clunky, but is is the approved way https://docs.microsoft.com/en-gb/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2015

Within your .csproj file, you will find lots of PropertyGroup nodes. At the end of the list of PropertyGroup nodes (position not critical), add another PropertyGroup node with this content:

<PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly> 
</PropertyGroup>

Now look near the end of the .proj file, and you will see a line like this:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

(For interest, on my computer with VS2017 on it that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.CSharp.targets)

Beneath that line, add a line like this:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />

(For interest, on my computer that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets)

YMMV. If yours is a web project, there is probably a line nearby that is similar but to do with Microsoft.WebApplication.targets, from which you can draw inspiration.

That, possibly with a restart of Visual Studio, should do it. If you delete the transformed file that your .tt file emits, and then do a rebuild of your project, you should see that the emitted file reappears.

like image 167
Jinlye Avatar answered Oct 14 '22 15:10

Jinlye


.tt files are only auto-run by VS on save. You can install AutoT4 to have them run before/after build. (Be aware that at the moment there is a limitation with the new .csproj files - the options don't show up for them in the properties window.)

If you've converted from the old project.json/.xproj format, you may need to add the template to the project explicitly:

<ItemGroup>
  <None Update="Foo.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <LastGenOutput>Foo.cs</LastGenOutput>
  </None>
  <Compile Update="Foo.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>Foo.tt</DependentUpon>
  </Compile>
</ItemGroup>

Related GitHub issue

Edit

As mentioned in the comments below, you can do this quickly & easily by excluding, then including the template in your project.

like image 42
Bennor McCarthy Avatar answered Oct 14 '22 13:10

Bennor McCarthy