向 T4 模板添加属性 - 选择服务器、数据库、表
各位,
我想创建一些 T4 模板,用于从数据库生成类文件(每个表大约 7 个),以支持我们内部的 ORM(别问 - 长话短说和历史原因......
)我真正想做的是在我的主 TT 模板上有一个属性,可以直观地选择要为其创建文件的服务器、数据库和表(类似于 CodeSmith 中的表选择器)。
由于这似乎不存在(或者确实存在?),我认为下一个最好的办法是使用服务器、数据库、表名的三个字符串属性,并使用 SMO 连接到该表并获取我需要的列数据。
我尝试遵循 Oleg Sych 的示例,并想出了:
<#@ property name="serverName" processor="PropertyProcessor" type="System.String" #>
<#@ property name="databaseName" processor="PropertyProcessor" type="System.String" #>
<#@ property name="tableName" processor="PropertyProcessor" type="System.String" #>
但是如何在连接到使用 SMO 指定的服务器来检索数据的代码块中引用这些属性?
<#
Server server = new Server();
Database database = new Database(server, "DASECO_DEV");
Table table = new Table(database, "T_User");
table.Refresh();
#>
我尝试将 <#= serverName #>
放在 Server() 构造函数的括号内 - 但这不起作用:-( 似乎我有点卡在这里...... ...如果我无法评估和使用它们的值,那么拥有属性有什么意义:-)
有接受者吗?
马克
Folks,
I'd like to create some T4 templates for generating class files (about 7 per table) from a database to support our in-house ORM (don't ask - long story and historical reasons.....)
What I'd really love to do is have a property on my main TT template to visually pick server, database and table for which to create the files (something like the table picker in CodeSmith).
Since that doesn't seem to exist (or does it?), I figured next best thing is using three string property for server, database, table name, and use SMO to connect to that table and get the column data I need.
I tried to follow Oleg Sych's examples, and came up with:
<#@ property name="serverName" processor="PropertyProcessor" type="System.String" #>
<#@ property name="databaseName" processor="PropertyProcessor" type="System.String" #>
<#@ property name="tableName" processor="PropertyProcessor" type="System.String" #>
but then how do I reference those properties in my code block which connects to the server specified using SMO to retrieve the data?
<#
Server server = new Server();
Database database = new Database(server, "DASECO_DEV");
Table table = new Table(database, "T_User");
table.Refresh();
#>
I tried putting a <#= serverName #>
inside the brackets of the Server() constructor - but that doesn't work :-( Seems like I'm a bit stuck here...... what's the point of having properties if I can't evaluate and use their values! :-)
Any takers??
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个怎么样?
How about this?