是否可以在 VS2010 中检索 DDL 生成模板内的连接字符串?

发布于 2024-08-22 23:50:09 字数 350 浏览 7 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与往事干杯 2024-08-29 23:50:09

如果这对其他人有帮助,这里是我创建的一个片段,用于从 T4 内部读取实体框架连接字符串。您向其传递模型名称(也是连接字符串的名称)。它找到并解析我需要的连接位。当它不成功时,它还会抛出有用的错误。

要使用:

A. 如果您还没有引用这些程序集,请将其粘贴到模板的顶部:

<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Configuration" #>

B. 将这个丑陋(但紧凑)的代码粘贴到模板的末尾:

<#+
string GetEFConnectionString(string modelName)
{
    string file = null, key = "provider connection string=\"";
    foreach (EnvDTE.ProjectItem item in ((EnvDTE.Project)((Array)((EnvDTE.DTE)((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE))).ActiveSolutionProjects).GetValue(0)).ProjectItems)
        if (System.Text.RegularExpressions.Regex.IsMatch(item.Name, "(app|web).config", System.Text.RegularExpressions.RegexOptions.IgnoreCase)) {
            file = item.get_FileNames(0); break;
        }
    if (file == null) throw new Exception("config file could not be found");
    var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(new System.Configuration.ExeConfigurationFileMap() { ExeConfigFilename = file }, System.Configuration.ConfigurationUserLevel.None);
    var cn = config.ConnectionStrings.ConnectionStrings[modelName];
    if (cn == null) throw new Exception(modelName + " connection string could not be found");
    string s = cn.ConnectionString;    
    int pos = s.IndexOf(key,StringComparison.OrdinalIgnoreCase);    
    if (pos<0) throw new Exception("could not find value '" + key + "' inside connection string");
    pos += key.Length;
    int pos2=s.IndexOf('"',pos);
    if (pos2 < 0) throw new Exception("could not find ending \" in connection string");
    return s.Substring(pos,pos2-pos);
}
#>

C. 像这样使用它:

using(var connection = new SqlConnection(GetEFConnectionString("Database"))) {
    ..
}    

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:

<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Configuration" #>

B. Paste this ugly (but compact) code at the end of your template:

<#+
string GetEFConnectionString(string modelName)
{
    string file = null, key = "provider connection string=\"";
    foreach (EnvDTE.ProjectItem item in ((EnvDTE.Project)((Array)((EnvDTE.DTE)((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE))).ActiveSolutionProjects).GetValue(0)).ProjectItems)
        if (System.Text.RegularExpressions.Regex.IsMatch(item.Name, "(app|web).config", System.Text.RegularExpressions.RegexOptions.IgnoreCase)) {
            file = item.get_FileNames(0); break;
        }
    if (file == null) throw new Exception("config file could not be found");
    var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(new System.Configuration.ExeConfigurationFileMap() { ExeConfigFilename = file }, System.Configuration.ConfigurationUserLevel.None);
    var cn = config.ConnectionStrings.ConnectionStrings[modelName];
    if (cn == null) throw new Exception(modelName + " connection string could not be found");
    string s = cn.ConnectionString;    
    int pos = s.IndexOf(key,StringComparison.OrdinalIgnoreCase);    
    if (pos<0) throw new Exception("could not find value '" + key + "' inside connection string");
    pos += key.Length;
    int pos2=s.IndexOf('"',pos);
    if (pos2 < 0) throw new Exception("could not find ending \" in connection string");
    return s.Substring(pos,pos2-pos);
}
#>

C. Use it like such:

using(var connection = new SqlConnection(GetEFConnectionString("Database"))) {
    ..
}    
岛歌少女 2024-08-29 23:50:09

我在一个 MSDN 论坛上发布了我的问题,并得到了 Lingzhi Sun 的回复,他给我指出了 skysanders.net 上的几个链接。这些链接中的第二个有一个非常好的示例,用于获取 app/web.config 文件,特别是我想要的部分,连接字符串。它没有提供有关我在原始问题中描述的场景的特定连接字符串的任何信息,但这让我足够接近。

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.

夏至、离别 2024-08-29 23:50:09

那么,EF 连接字符串将始终与模型具有相同的名称,对吧? DB 连接字符串将嵌入到 EF 连接字符串中。所以我想说你应该能够通过 EF 连接字符串获取它,至少是间接获取它。

因为您没有在程序集中运行,所以必须指定配置文件名。

所以它会是这样的:

var config = ConfigurationManager.OpenExeConfiguration(name);
var cs = config.ConnectoinStrings[modelName];

请注意,这里的 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:

var config = ConfigurationManager.OpenExeConfiguration(name);
var cs = config.ConnectoinStrings[modelName];

Note that name, here, is supposed to be an EXE name. But in the IDE, your config fine is going to be called App.config rather than MyApp.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文