App.config 似乎被忽略

发布于 2024-10-09 00:42:22 字数 1307 浏览 2 评论 0原文

作为重构操作的结果,我有了这个类库。 我添加了一个 App.config 文件并添加了如下内容:

<configuration>
    <connectionStrings>
        <add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

但是当我运行应用程序时,调试发现这完全被忽略了。立即窗口告诉我:

ConfigurationManager.ConnectionStrings[0]
                 {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    Key: "LocalSqlServer"
    Name: "LocalSqlServer"
    Properties: {System.Configuration.ConfigurationPropertyCollection}
    ProviderName: "System.Data.SqlClient"

我已经检查了 bin 目录中生成的配置文件,其内容与 App.config 相同。

我尝试使用以下方式读取 App.config:

ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString

我想说没有什么异常,但是出了什么问题?

I've this Class Library, as a result of a refactor action.
I added an App.config file and added something like this:

<configuration>
    <connectionStrings>
        <add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

But when I run the application, debugging learns me this is totally ignored. The immediate window tells me:

ConfigurationManager.ConnectionStrings[0]
                 {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    Key: "LocalSqlServer"
    Name: "LocalSqlServer"
    Properties: {System.Configuration.ConfigurationPropertyCollection}
    ProviderName: "System.Data.SqlClient"

I've checked generated config file in the bin directory and its contents are identical to the App.config.

I try to read the App.config using:

ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString

Nothing out of the ordinary I'd say, but what is going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

得不到的就毁灭 2024-10-16 00:42:22

类库没有自己的配置;对于名为 Foo.exe 的应用程序,您需要将配置放在 Foo.exe.config 中。这里的例外是 Web 应用程序,其中 web.config 是命名约定。

A class library doesn't get its own config; for an app named Foo.exe you need your configuration to be in Foo.exe.config. The exception here is web apps, where web.config is the naming convention.

裂开嘴轻声笑有多痛 2024-10-16 00:42:22

此连接字符串应该位于您正在运行的 exe 的相应 app.config 中。

This connectionString should be in the corresponding app.config of the exe that you are running.

不乱于心 2024-10-16 00:42:22

文件是否被复制到输出?其次,应该可以从代码生成的 AssemblyName.Settings.Properties 类访问连接字符串。

Is the file being copied to output? Secondly the connection string should be accessible from the AssemblyName.Settings.Properties class which is generated from your code.

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