views:

123

answers:

2

Hi all,

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.

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) : 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?

Thanks

Matt.

A: 

setting.ttinclude needs to be run inside the project of visual studio, if you want to run from command line you will have to give the settings file the connection string rather than pointing to it in the config files, you may also have to set a project path.

you seee it calling this:

string GetConnectionString(string connectionStringName){
    var _CurrentProject = GetCurrentProject();

    string result="";
    ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
    configFile.ExeConfigFilename = GetConfigPath();

thats probably where the problem stems, so if you set these by hand then you oproblem could then be solved

minus4
+1  A: 

I wrote a blog entry that covers this ground somewhat:

Options for running T4 templates from .NET code

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".

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.

Michael Maddox
Thanks for this, I'll try to edit the template next time I get some time and see if I can resolve the problem with this information
Matt Roberts