views:

143

answers:

2

I am playing around with creating a T4 template for the "DDL Generation Template option" (model first) process in Visual Studio 2010 RC. Is it possible to retrieve the connection string that is associated with that process? If I right click on the .edmx file and choose "Generate Database from Model..." I have the option of choosing a data connection. That connection string is saved to the app.config (assuming that the option is checked). So I am wondering if it is possible to retrieve that connection string inside the T4 template. I would like to generate different information from the template based on the connection string.

More generally, is it possible to get any context information in this situation? So far, the only thing I have successfully retrieved is the .NET data provider name.

Note - I have studied the ideas provided by Craig but am only getting the name of the IDE (devenv.exe), which quite possibly means I am just doing something wrong.

+1  A: 

Well, the EF connection string will always have the same name as the model, right? The DB connection string will be embedded in the EF connection string. So I'd say you should be able to get it, at least indirectly, via the EF connection string.

Because you're not running in the assembly, have to specify the config file name.

So it would be something like:

var config = ConfigurationManager.OpenExeConfiguration(name);
var cs = config.ConnectoinStrings[modelName];

Note that name, here, is supposed to be an EXE name. But in the IDE, your config fine is going to be called App.config rather than MyApp.dll.config. So you may have to play around with this to get it to work -- try using "App" as the EXE name!

Worst case is open it as a file and then use the config manager.

Craig Stuntz
I believe that is correct. But I'm not even smart enough to figure out how to get the EF connection string. I've examined everything I could find in the debugger inside the template, tried to find documentation with no success and looked at the templates that shipped with the RC. I'm either blind or something. Do you have a hint on getting the EF connection string?
Mark Wilkins
I'll update the answer with more.
Craig Stuntz
This seems like the correct approach, so I am upvoting it. However, I am still stuck and have to work on some other stuff at the moment. The currently running application is devenv.exe, so I am having no luck at getting to the project's app.config. The link must exist, so I will keep exploring later.
Mark Wilkins
I wasn't clever enough to figure out how to run with your answer and make it work. But it is a nudge in the right direction. I was able to find the answer via the MSDN forums. So I answered my own question with that information. But since yours was a good start, I am selecting it as the accepted answer (plus I don't want the bounty points to go to waste).
Mark Wilkins
+1  A: 

I posted my question on one of the MSDN forums and got a response from Lingzhi Sun who pointed me in the direction of a couple of links at skysanders.net. The second of these links has a very nice example of getting to the app/web.config file and, specifically the part I wanted, the connection strings. It doesn't give any information on the specific connection string for the scenario I described in the original question, but this gets me close enough.

Mark Wilkins

related questions