.Net Standard 2.0 ConfigurationManager Doesn't Read Connection Strings

笑着哭i 提交于 2019-12-11 05:06:55

问题


I have a .Net Standard 2.0 Library. I've references System.Configuration.ConfigurationManager via NuGet and I've added an App.config file with two connection strings. When I break on the following line, I notice that ConnectionStrings only contains a single connection string which is neither of the connection strings I have in my App.Config (shown below).

var foo = ConfigurationManager.ConnectionStrings["cs1"].ConnectionString;

The single connection string which does appear in ConnectionStrings looks like the following:

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

I've tried changing the properties on my App.config to "copy always" and changing the build action on the file. I've tried including and excluding the providerName attribute on the add tags. I did come across a SO post which describes an error involving the aforementioned connection string, though the accepted answer does not work for me (.NET 2.0 App.Config connection strings includes unwanted SQLExpress default). I've not been able to find any other documentation of this error anywhere else online.

This leads me to believe the problem is that ConfigurationManager isn't reading my App.config at all, which is why I tried changing the copy settings, etc. for the file.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <clear/>
        <add name="cs1" connectionString="Valid Connection String 1;" />
        <add name="cs2" connectionString="Valid Connection String 2;" />
    </connectionStrings>
</configuration>

The actual result is that ConfigurationManager.ConnectionStrings has only a single item which is the aforementioned connection string, whereas the desired result is that ConfigurationManager.ConnectionStrings has two connection strings which are those I've defined in my App.config file.

Thanks in advance for the help!

来源:https://stackoverflow.com/questions/57377782/net-standard-2-0-configurationmanager-doesnt-read-connection-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!