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

扶醉桌前 提交于 2019-11-30 22:42:35

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!