Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding properties to T4 template - picking server, database, table

Tags:

properties

t4

smo

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

like image 658
marc_s Avatar asked Feb 25 '26 17:02

marc_s


1 Answers

How about this?

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!