如何使T4模板可用连接字符串? [英] How to make connection strings available in a T4 template?

查看:225
本文介绍了如何使T4模板可用连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,我实例化一个EF上下文来读取一些数据的T4模板。
的问题是,上下文无法看到从Web.config的连接字符串。



如何使从Web.config连接字符串提供给?模板



更多信息:




  • 模板从头开始编写

  • EF EDMX位于另一个项目

  • 我做模板第一个查询告诉我,它无法找到所需的连接字符串中的项目,其中模板所在






尝试下面的一些解决方案(谢谢),但我得到这个

 错误2编译转型:Microsoft.VisualStudio.TextTemplating12165CB53B43A726CBA54A29800255D257AAFD4D5F0DACE4DFE5872F2DEC7739EDF358F49C0444A912B851939903A79BC6180FCEB3FD0D1BF8C0093741DDDACA.GeneratedTextTransformation'不包含'主机'和一个没有定义扩展方法主机接受型Microsoft.VisualStudio.TextTemplating12165CB53B43A726CBA54A29800255D257AAFD4D5F0DACE4DFE5872F2DEC7739EDF358F49C0444A912B851939903A79BC6180FCEB3FD0D1BF8C0093741DDDACA.GeneratedTextTransformation'的第一个参数可以找到(是否缺少using指令或程序集引用?)

我有以下声明:

 <#@总成NAME =EnvDTE#> 
<#@导入命名空间=EnvDTE#>
<#@导入命名空间=Microsoft.VisualStudio.TextTemplating#>

如果我包括大会Microsoft.VisualStudio.TextTemplating它告诉我,它已经插入。



此外,有没有什么办法能让ConfigurationManager中提供给的DbContext这样他就可以读W / E,他希望没有我在幕后通过他的连接字符串后面?






解决了,再次感谢:

 <#@模板hostspecific =真LANGUAGE =C#调试=假#> 
<#@集名称=System.Core.dll#>
<#@集名称=System.Configuration#>
<#@导入命名空间=Microsoft.VisualStudio.TextTemplating#>
<#@导入命名空间=System.Configuration#>

VAR地图=新ExeConfigurationFileMap();
map.ExeConfigFilename = this.Host.ResolvePath(@.. \Web.config);

无功配置= ConfigurationManager.OpenMappedExeConfiguration(地图,ConfigurationUserLevel.None);
变种的connectionString = config.ConnectionStrings.ConnectionStrings [MyConnectionName]的ConnectionString。


解决方案

我访问的连接字符串从App.config中从T4模板在接下来的方式:

 <#@模板调试=假hostspecific =真LANGUAGE =C# #> 

ExeConfigurationFileMap configFileMap =新ExeConfigurationFileMap(this.Host.ResolvePath(@.. \ProjName\App.config));
configFileMap.ExeConfigFilename = this.Host.ResolvePath(@.. \ProjName\App.config);
配置配置= ConfigurationManager.OpenMappedExeConfiguration(configFileMap,ConfigurationUserLevel.None);
字符串的connectionString = config.ConnectionStrings.ConnectionStrings [0] .ConnectionString;


I've written a T4 template where I instantiate an EF context to read some data. The problem is that the context cannot see the connection string from the Web.config.

How can I make the connection strings from the Web.config available to the template?

More info:

  • template is written from scratch
  • EF edmx is located in another project
  • the first query I make in the template tells me that it cannot find the required connection string in the project where the template resides

Tried some of the solutions (thank you) below but I get this:

Error 2 Compiling transformation: 'Microsoft.VisualStudio.TextTemplating12165CB53B43A726CBA54A29800255D257AAFD4D5F0DACE4DFE5872F2DEC7739EDF358F49C0444A912B851939903A79BC6180FCEB3FD0D1BF8C0093741DDDACA.GeneratedTextTransformation' does not contain a definition for 'Host' and no extension method 'Host' accepting a first argument of type 'Microsoft.VisualStudio.TextTemplating12165CB53B43A726CBA54A29800255D257AAFD4D5F0DACE4DFE5872F2DEC7739EDF358F49C0444A912B851939903A79BC6180FCEB3FD0D1BF8C0093741DDDACA.GeneratedTextTransformation' could be found (are you missing a using directive or an assembly reference?)

I have the following declared:

<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>

If I include the assembly for Microsoft.VisualStudio.TextTemplating it tells me that it's already inserted.

Also, is there any way to make the ConfigurationManager available to the DbContext so he can read w/e he wants behind the scenes without me passing him the connection string?


Solved it, thanks again:

<#@ template hostspecific="true" language="C#" debug="false" #>
<#@ assembly name="System.Core.dll" #>
<#@ assembly name="System.Configuration" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="System.Configuration" #>

var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = this.Host.ResolvePath(@"..\Web.config");

var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var connectionString = config.ConnectionStrings.ConnectionStrings["MyConnectionName"].ConnectionString;

解决方案

I accessed a connection string from App.config from T4 template in the next way:

<#@ template debug="false" hostspecific="true" language="C#" #>

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(this.Host.ResolvePath(@"..\ProjName\App.config"));
configFileMap.ExeConfigFilename = this.Host.ResolvePath(@"..\ProjName\App.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
string connectionString = config.ConnectionStrings.ConnectionStrings[0].ConnectionString;

这篇关于如何使T4模板可用连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆