是否可以在 VS2010 中检索 DDL 生成模板内的连接字符串?
我正在尝试为 Visual Studio 2010 RC 中的“DDL 生成模板选项”(模型优先)过程创建 T4 模板。是否可以检索与该进程关联的连接字符串?如果我右键单击 .edmx 文件并选择“从模型生成数据库...”,我可以选择数据连接。该连接字符串将保存到 app.config(假设已选中该选项)。所以我想知道是否可以在 T4 模板内检索该连接字符串。我想根据连接字符串从模板生成不同的信息。
更一般地说,在这种情况下是否可以获得任何上下文信息?到目前为止,我唯一成功检索到的是 .NET 数据提供程序名称。
注意 - 我已经研究了 Craig 提供的想法,但只得到了 IDE 的名称 (devenv.exe),这很可能意味着我只是做错了什么。
I am playing around with creating a T4 template for the "DDL Generation Template option" (model first) process in Visual Studio 2010 RC. Is it possible to retrieve the connection string that is associated with that process? If I right click on the .edmx file and choose "Generate Database from Model..." I have the option of choosing a data connection. That connection string is saved to the app.config (assuming that the option is checked). So I am wondering if it is possible to retrieve that connection string inside the T4 template. I would like to generate different information from the template based on the connection string.
More generally, is it possible to get any context information in this situation? So far, the only thing I have successfully retrieved is the .NET data provider name.
Note - I have studied the ideas provided by Craig but am only getting the name of the IDE (devenv.exe), which quite possibly means I am just doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这对其他人有帮助,这里是我创建的一个片段,用于从 T4 内部读取实体框架连接字符串。您向其传递模型名称(也是连接字符串的名称)。它找到并解析我需要的连接位。当它不成功时,它还会抛出有用的错误。
要使用:
A. 如果您还没有引用这些程序集,请将其粘贴到模板的顶部:
B. 将这个丑陋(但紧凑)的代码粘贴到模板的末尾:
C. 像这样使用它:
In case this helps anyone else, here is a snippet I created to read the Entity Framework connection string from inside T4. You pass it the model name (which is also the name of the connection string). It finds and parses just the connection bit I need. It also throws helpful errors when it does not succeed.
To use:
A. Paste this at the top of your template if you aren't already referencing these assemblies:
B. Paste this ugly (but compact) code at the end of your template:
C. Use it like such:
我在一个 MSDN 论坛上发布了我的问题,并得到了 Lingzhi Sun 的回复,他给我指出了 skysanders.net 上的几个链接。这些链接中的第二个有一个非常好的示例,用于获取 app/web.config 文件,特别是我想要的部分,连接字符串。它没有提供有关我在原始问题中描述的场景的特定连接字符串的任何信息,但这让我足够接近。
访问T4 模板中的 app.config/web.config
从 T4 模板访问 app.config/web.config - 拍摄 2
I posted my question on one of the MSDN forums and got a response from Lingzhi Sun who pointed me in the direction of a couple of links at skysanders.net. The second of these links has a very nice example of getting to the app/web.config file and, specifically the part I wanted, the connection strings. It doesn't give any information on the specific connection string for the scenario I described in the original question, but this gets me close enough.
Accessing app.config/web.config from T4 template
Accessing app.config/web.config from T4 template - Take 2
那么,EF 连接字符串将始终与模型具有相同的名称,对吧? DB 连接字符串将嵌入到 EF 连接字符串中。所以我想说你应该能够通过 EF 连接字符串获取它,至少是间接获取它。
因为您没有在程序集中运行,所以必须指定配置文件名。
所以它会是这样的:
请注意,这里的
name
应该是一个 EXE 名称。但在 IDE 中,您的配置文件将被称为App.config
而不是MyApp.dll.config
。因此,您可能需要尝试一下才能使其正常工作 - 尝试使用“App”作为 EXE 名称!最坏的情况是将其作为文件打开,然后使用配置管理器。
Well, the EF connection string will always have the same name as the model, right? The DB connection string will be embedded in the EF connection string. So I'd say you should be able to get it, at least indirectly, via the EF connection string.
Because you're not running in the assembly, have to specify the config file name.
So it would be something like:
Note that
name
, here, is supposed to be an EXE name. But in the IDE, your config fine is going to be calledApp.config
rather thanMyApp.dll.config
. So you may have to play around with this to get it to work -- try using "App" as the EXE name!Worst case is open it as a file and then use the config manager.