1
Vote

Accessing Connection Strings From T4 Template

description

My template creates an instance of the DBContext used by the default project to customize the template output based on the data in the database, not the schema of the database.

To create the instance my template file needs access to the connection string.

Question: Short of hard coding the connection string into the t4 template file, what's the best way of providing it from an external config file?

Ideally it would be coming from the Package Manager Console's Default Project's config file.

comments

TonesNotes wrote Feb 21, 2014 at 2:18 PM

Punted and went for parsing the config file manually.
Added the following method to a t4 include file:
<#+
string GetConnectionString(string configFile) {
    var doc = new System.Xml.XmlDocument();
    doc.Load(configFile);
    var node = doc.SelectSingleNode("/configuration/connectionStrings/add[@name='MyEntities']");
    return node.Attributes["connectionString"].Value;
}
#>