Design-time T4 templates in ASP.NET 5 (VS 2015)

2019-02-14 19:16发布

问题:

I can't seem to find a way to make T4 templates in VS 2015 RTM, in an ASP.NET 5 (vNext) project.

I even installed the T4 toolbox for Visual Studio 2015 extension, but the tt templates are not transformed.

The property Custom Tool doesn't appear in the tt file properties, neither can't I find the 'Run Custom Tool' command.

Update

The reason I want the T4 templates, is the introduction of the config.json file, and the pluggable configuration system, which is an awesome thing, but with the price of not having the setting properties strongly-typed.
I've read this article that explains how to achive this, but there is still no generation. Since I have a pretty complex configuration structure, I thought about making a T4 template that will generate an AppSettings file. Any ideas on that are obviously welcome too.

回答1:

The ASP.Net 5 (vnext) project is a completely new animal and technically still in beta, its not scheduled for RC til November 2015. Also it's attempting to be completely cross platform so initially the team favored using razor templates instead of T4 for scaffolding. They had no plans to support T4 (or any single file generators) at all until an out cry from the community made them change their mind. According to that thread they will support it but have given no dates. They do seem to have made progress, back in January when I was testing my T4 extension I had issues with the project file(now in json format) not supporting custom properties but as of the release on 7/20/2015 it seems to work now. The engine for running T4 inside of visual studio 2015 is still there so you can use it if you like from other project types. You can create a console app and have it store the T4 files but generate them in the vnext project. If you want a cleaner solution you can also try out my extension T4 Awesome, it gives you a way to organize and call your templates via right click menus.



回答2:

I've found that I can still use the MSBuild targets that ship with the Modeling SDK for Microsoft Visual Studio 2015 when building an ASP.NET 5 project with ASP.NET 5 RC1.

I got there by modifying the directions from MSDN - Code Generation in a Build Process.

  1. Add the *.tt and the output files (*.cs in my case) to the project folder structure.
  2. Unload and edit the project's *.xproj file.
  3. Add the following Import element after the Microsoft.DNX.targets import:

    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
    
  4. Add an ItemGroup element similar to the following (I added this immediately before the import statements):

    <ItemGroup>
      <Content Include="MyTemplate.tt">
        <Generator>TextTemplatingFileGenerator</Generator>
        <LastGenOutput>MyTemplate.cs</LastGenOutput>
      </Content>
    </ItemGroup>
    
  5. Optionally, you can add elements to the Globals PropertyGroup element to control the transformation task:

    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    
  6. Reload the project's *.xproj and build normally.

The templates I'm using are pretty simple, so there might be limitations of this approach that I'm missing out on.



回答3:

If you watch Julie Lerman's pluralsight video: http://www.pluralsight.com/courses/entity-framework-7-looking-ahead she addresses this. There are no plans as of now to remove the T4 templates in upcoming versions of Visual Studio but they didn't make it in for the release. You code always run the reverse engineer tool on your database and go with a code first approach and switch back later (though I don't know why you would in my opinion) but that would be a work around until there is more information on the T4 templates in current VS versions.