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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With