Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Templates and connection strings in a class library

I'm using the template posted in this thread to generate C# enums from a couple of lookup tables in SQL Server within a class library that contains my DAL.

At the moment, I have the connection string used by the templates embedded an a template include file in the class library. Is there a convenient way to have the template grab the connection string from the main project (WAP)'s web.config without having to include a physical path? Or is there a better way to approach this?

Edit

I've also considered creating a SQL CLR assembly which returns a table-valued function containing the enum contents (which would then be defined in C#, not in the database), but I'm not sure what the performance hit would be. Whether or not it's significant will obviously be application-dependent but I'd hate to charge down a crappy path if it's a know best-avoid-this approach.

like image 261
3Dave Avatar asked Jan 05 '11 17:01

3Dave


People also ask

What is a connection string in database?

A connection string is a string that contains information about a data source (usually a database engine), as well as the information necessary to connect to it.

What is connection string in Visual Studio?

A connection string (hereafter CS) is a string which is used to gain access to different data sources from a VB . Net or a C# . Net application. The data sources can be MS SQL Server, MS Access or SQL Server on remote server.


1 Answers

I used the following approach for reading from the web.config when executing the T4 template

<# var path = Host.ResolvePath(@"..\..\www"); #>

where ..\..\www is the relative path to the directory where my web.config is located in relation to where my T4 template is being executed

var config = ConfigurationManager.OpenMappedExeConfiguration(
   new ExeConfigurationFileMap { ExeConfigFilename = location +@"\web.config" },
   ConfigurationUserLevel.None);

var connStrings = config.ConnectionStrings;
like image 165
Pauli Østerø Avatar answered Nov 03 '22 01:11

Pauli Østerø