如何自动为SubSonic生成t4代码 [英] How can I automate the t4 code generation for SubSonic

查看:121
本文介绍了如何自动为SubSonic生成t4代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SubSonic 3(ActiveRecord模式)为我的DAL生成代码.它使用T4模板(.tt)文件,这些文件一经保存,便会为您生成代码.

I'm using SubSonic 3 (ActiveRecord mode) to generate the code for my DAL. It uses T4 templates (.tt) files that as soon as you save, generate the code for you.

作为NANT构建的一部分,我想使它自动化,但是我无法使其正常工作.我知道MS提供了一个名为TextTransform的工具来从T4模板生成代码,但是在SubSonic模板的情况下,这似乎不起作用-我认为模板对在Visual Studio中运行该模板做出了一些假设,从命令行似乎不起作用.当我尝试对ActiveRecord.tt运行它时收到的错误是:

I want to automate this as part of my NANT build, but I can't get this to work. I know that MS provide a tool called TextTransform to generate the code from T4 templates, but in the case of the SubSonic templates this doesn't seem to work - I think the templates make some assumptions about the template being run from within Visual Studio, which doesn't seem to work from the command line. The error I get when I try to run it against ActiveRecord.tt is:

Subsonic \ ActiveRecord.tt(0,0):错误:正在运行转换:System.InvalidCastException:无法将类型为"Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost"的对象转换为类型为"System.IServiceProvider". 在Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b中. c:\ Users \ matt.roberts \ AppData \ Loc中的GeneratedTextTransformation.GetCurrentProject() al \ Temp \ subsonic \ Settings.ttinclude:第103行 在Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b中.在c:\ U中的GeneratedTextTransformation.GetConnectionString(String connectionStringName) sers \ matt.roberts \ AppData \ Local \ Temp \ subsonic \ Settings.ttinclude:line 51 在Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b.在c:\ Users \ matt.roberts \ AppData \中的GeneratedTextTransformation.get_ConnectionString() Local \ Temp \ subsonic \ Settings.ttinclude:第87行

Subsonic\ActiveRecord.tt(0,0) : error : Running transformation: System.InvalidCastException: Unable to cast object of type 'Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost' to type 'System.IServiceProvider'. at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetCurrentProject() in c:\Users\matt.roberts\AppData\Loc al\Temp\subsonic\Settings.ttinclude:line 103 at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetConnectionString(String connectionStringName) in c:\U sers\matt.roberts\AppData\Local\Temp\subsonic\Settings.ttinclude:line 51 at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.get_ConnectionString() in c:\Users\matt.roberts\AppData\ Local\Temp\subsonic\Settings.ttinclude:line 87

有人成功实现了这一代的自动化吗?

Has anyone managed to automate this generation?

谢谢

马特.

推荐答案

我写了一篇博客文章,涵盖了这一方面:

I wrote a blog entry that covers this ground somewhat:

从中运行T4模板的选项. NET代码

要执行您想做的事情,将需要对SubSonic T4模板进行一些手术.特别是,您必须(至少)用不需要运行Visual Studio的内容替换对EnvDTE.DTE的所有引用.如果您只是想使其正常工作",那就像将一些路径和/或配置信息硬编码到T4模板中一样简单.

To do what you are trying to do will require some surgery on the SubSonic T4 templates. Specifically, you'll have to (minimally) replace all references to EnvDTE.DTE with something that doesn't require Visual Studio to be running. That may be as simple as hard coding some paths and/or configuration information into your T4 templates if you want to just "get it working".

重要的是要记住,T4模板是.NET代码的较薄包装.如果可以从.NET进行操作,则很有可能可以从T4进行操作,您只需要了解如何在T4提供的不同约束集内工作即可.不同的T4主机"(如Visual Studio与TextTransform.exe)的行为方式不同,仅仅是T4模板在一台主机下运行良好并不意味着它在另一台主机下运行良好.

It's important to remember that T4 templates are a somewhat thin wrapper around .NET code. If you can do it from .NET, you can very likely do it from T4, you just have to understand how to work within the different set of constraints that T4 provides. Different T4 "hosts" (like Visual Studio vs. TextTransform.exe) behave in different ways and just because a T4 template runs fine under one host doesn't not mean it will run fine under another host.

这篇关于如何自动为SubSonic生成t4代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆