Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen creating a T4 template, how can the app config and other file resources be utilized?
    primarykey
    data
    text
    <p>I have a T4 template that I am trying to create that will code gen lookup values from a database via Nhibernate. My problem is my data access layer uses the path of the Nhibernate configuration in order to compile the configuration upon startup (a static constructor). </p> <p>I don't know how to make t4 "see" this file path so that when my code gen runs it can get this configuration file. Nor do I know how to make t4 "see" my configuration manager class; which contains the app setting that lists the path to the nhibernate xml file. </p> <p>I have two configuration files, one for SQL Server and one for sqlite. The configuration file needs to be in the root directory of the executing assembly in order to nhibernate to compile the configuration.</p> <p>It seems like my template won't be able to use the high level business layer to select the data from the database, rather I may have to copy all the nhibernate configuration code into the template as well (ugh).</p> <p>My DB wrapper:</p> <pre><code>private class DBSingleton { static DBSingleton() { string path = ConfigurationManager.AppSettings["DBConfigFileFullPath"]; NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(path); cfg.AddInputStream(HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetAssembly(typeof(Plan)))); instance = cfg.BuildSessionFactory(); } internal static readonly ISessionFactory instance; } </code></pre> <p>And my template:</p> <pre><code>&lt;# System.Diagnostics.Debugger.Launch(); string path = Host.ResolvePath(@"..\DB.UnitTest\App.config"); System.Configuration.ConfigurationManager.AppSettings.Add( "DBConfigFileFullPath", path); //System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(path); //Path to your config file //System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap); ReferenceValueBL bl = new ReferenceValueBL(); List&lt;ReferenceValue&gt; refVals = bl.Select(); &lt;- problem occurs here foreach(ReferenceValue rv in refVals) { Write("Public "); Write(rv.ReferenceValueCode.GetType().Name); Write(" "); Write(rv.ReferenceValueCode); Write(" = "); Write(rv.ReferenceValueCode); } #&gt; </code></pre> <p>My problem occurs when the bl variable tries to call select(). That's when the DBSingleton is initialized. It throws an error saying the app setting is null. If I hard code the relative file path in the DB class to just be ./Dbconfig.xml it still throws an error because the executing assembly doesn't have that file in it's local directory.</p> <p>How do other people handle getting t4 to use app/web config files without reading from the config file within the template and then injecting a connection string into the DAL? I don't have that luxury. The file has to be either placed in a readable location or t4 has to know to look somewhere.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload