7

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.

Community
  • 1
  • 1
3Dave
  • 28,657
  • 18
  • 88
  • 151

1 Answers1

10

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;
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
  • I was hoping not to have to have to hard code paths of any kind, but seems like that's not going to happen. Thanks. – 3Dave Jan 05 '11 at 21:50
  • 3
    of course you need to tell the template how its located relative to your config, but thats not considered hardcoding... you can move your solution as crazy as you want, as long as the relative location between the projects is the same it will still work. – Pauli Østerø Jan 06 '11 at 00:32