gpt4 book ai didi

c# - 为什么 ConfigurationManager.OpenMappedExeConfiguration 有一个额外的连接字符串不在文件中?

转载 作者:太空狗 更新时间:2023-10-29 20:49:48 25 4
gpt4 key购买 nike

我正在为一个 win 表单应用程序对我的配置文件进行单元测试。在 LocalTestRun.testrunco​​nfig 中,我将其设置为复制 app.config。我在测试运行时检查了 Environment.CurrentDirectory 并且该文件也没有这个额外的连接字符串。这是测试方法:

[TestMethod]
public void Configuration_ConnectionStrings_Connect()
{
Console.WriteLine(Environment.CurrentDirectory);
Assert.IsTrue(System.IO.File.Exists("App.Config"));
var configFileMap = new ExeConfigurationFileMap(){ ExeConfigFilename = "App.Config"};

var config= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

foreach (ConnectionStringSettings connectionString in config.ConnectionStrings.ConnectionStrings)
{
//Assumes all connections are to Sql server, test must be updated if not
Assert.AreEqual("System.Data.SqlClient",connectionString.ProviderName,"Test was only designed for SQL clients");
using (var cn=new System.Data.SqlClient.SqlConnection(connectionString.ConnectionString))
{
cn.Open();
Assert.AreEqual(System.Data.ConnectionState.Open,cn.State);
}
}

}

但是,当我迭代 config.ConnectionStrings.ConnectionStrings 时,我有一个不在文件中的额外配置字符串:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

这不是也从来不是 asp.net 项目。

这是配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="HLIT_Ticketing.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="HLIT_Ticketing.Properties.Settings.HomeQConnectionString"
connectionString="Data Source=******\*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=******"
providerName="System.Data.SqlClient" />
</connectionStrings>
<userSettings>
<HLIT_Ticketing.Properties.Settings>
<setting name="AssociateRole" serializeAs="String">
<value>Associate</value>
</setting>
<setting name="DeveloperRole" serializeAs="String">
<value>Developer</value>
</setting>
</HLIT_Ticketing.Properties.Settings>
</userSettings>

这个额外的连接字符串来自哪里?

最佳答案

该连接字符串在机器级配置文件 machine.config 中定义。

如果您查看文件 C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config,您将看到以下部分:

<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

关于c# - 为什么 ConfigurationManager.OpenMappedExeConfiguration 有一个额外的连接字符串不在文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2029094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com