How can I automate t4 code generation for SubSonic

I am using SubSonic 3 (ActiveRecord mode) to generate code for my DAL. It uses T4 templates (.tt) files, which, as soon as you save, generate code for you.

I want to automate this as part of my NANT build, but I cannot get this to work. I know that MS provides a TextTransform tool for generating code from T4 templates, but in the case of SubSonic templates this does not work - I think the templates make some assumptions about starting a template from Visual Studio, which does not seem to work from the command line. The error I get when I try to start it using ActiveRecord.tt:

Subsonic \ ActiveRecord.tt (0,0): error: Conversion started: System.InvalidCastException: cannot reset object of type "Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost" to enter "System.IServiceProvider". at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetCurrentProject () in c: \ Users \ matt.roberts \ AppData \ Loc al \ Temp \ subsonic \ Settings.ttinclude: line 103 in Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetConnectionString (String connectionStringName) in c: \ U sers \ matt.roberts \ AppData \ Local \ Temp \ subsonic \ Settings.ttinclude: line 51 in Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.get_ConnectionString () in c: \ Users \ matt.roberts \ AppData \ Local \ Temp \ subsonic \ Settings.ttinclude: line 87

Has anyone been able to automate this generation?

thanks

Mt.

+2
source share
2 answers

I wrote a blog post that covers this area a bit:

Options for running T4 templates from .NET code

To do what you are trying to do, some operation on the SubSonic T4 templates will be required. In particular, you need to (minimally) replace all references to EnvDTE.DTE with something that does not require Visual Studio to start. It can be as simple as hard-coding some paths and / or configuration information into your T4 templates if you just want to “get it working”.

It is important to remember that T4 templates are a somewhat thin shell around .NET code. If you can do it with .NET, you are most likely to do it with T4, you just need to understand how to work within the many limitations that T4 provides. Different T4 hosts (for example, Visual Studio vs. TextTransform.exe) behave differently and just because the T4 template works fine under one host does not mean that it will work under a different host.

+4
source

setting.ttinclude needs to be run inside the visual studio project, if you want to run from the command line, you will have to specify the settings file in the connection string, and not point to it in the configuration files, you can also set the path to the project.

you see that it calls this:

string GetConnectionString(string connectionStringName){ var _CurrentProject = GetCurrentProject(); string result=""; ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); configFile.ExeConfigFilename = GetConfigPath(); 

probably where the problem arises, so if you install them manually, then you can solve the problem

+1
source

All Articles