tags:

views:

1104

answers:

4

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

+1  A: 

How about this?

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

Hi Oleg,

thanks for your tip - it's so simple and almost embarassingly easy.... :-)

Unfortunately, when doing this and then specifying "(local)" for server and my other items for database and table name, I get this error:

Error 1 Running transformation: Microsoft.SqlServer.Management.Common.InvalidPropertyValueException: Cannot apply value ‘null’ to property ServerInstance: Value cannot be null.. at Microsoft.SqlServer.Management.Common.ConnectionSettings.ThrowIfInvalidValue(String str, String propertyName, Boolean checkEmpty) at Microsoft.SqlServer.Management.Common.ConnectionSettings.set_ServerInstance(String value) at Microsoft.SqlServer.Management.Smo.ExecutionManager..ctor(String name) at Microsoft.SqlServer.Management.Smo.Server..ctor(String name)

"value cannot be null":....... there is no NULL! All the values are specified.... hmm.....

Marc

marc_s
A: 

So, how are you specifying the property values? There is no standard implementation for the property directive and no standard way to specify the property values. The error you are getting is probably the result of property values being specified incorrectly. You could make it work for a particular host (more here) at the cost of making the template host-specific. I gave up using this directive because of this. Encapsulating a template in a nested class and defining code generation parameters as its properties is a lot easier and more reliable (more here).

I'm clicking in the body of the *.tt file, and then the properties show up in the Properties window of VS08. I enter the strings for server, database, and table there.
marc_s
A: 

Well, it seems that the T4 templates are just a bit less capable and less "configurable" than the corresponding CodeSmith stuff. As long as the T4 stuff is good enough for you - great - but I'm baffled there's no way to make even simple things like externally set parameters to pass in value for e.g. server, database and table name work in T4.

Oh well - maybe T4 v2.0 in VS2010 will be a tad more useful!

marc_s