向 T4 模板添加属性 - 选择服务器、数据库、表

发布于 2024-07-13 11:04:37 字数 1003 浏览 4 评论 0原文

各位,

我想创建一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-07-20 11:04:37

这个怎么样?

<#    
    Server server = new Server(serverName);    
    Database database = new Database(server, databaseName);    
    Table table = new Table(database, tableName);    
    table.Refresh();
#>

How about this?

<#    
    Server server = new Server(serverName);    
    Database database = new Database(server, databaseName);    
    Table table = new Table(database, tableName);    
    table.Refresh();
#>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文