Retrieving a ConnectionString through System.Configuration Throws Exception

丶灬走出姿态 提交于 2019-11-29 18:51:47

The error you got relates to a configuration file that has incorrect content. Most likely, your app.config (if desktop) or web.config (if web app) file the following error:

It does not have as its first element the <configSections> element.

Check that file and make sure it looks something like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- Section groups, and stuff like userSettings, etc  -->
    </configSections>

    <connectionStrings>
        <add name="connectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integr‌​ated Security=True"/>
    </connectionStrings>

    <!-- Possibly other stuff  -->

</configuration>

Also ran into the same problem. The explanation Alex provided gives a clue into the issue however it is not enough.

The app.config apparently expects the elements to appear in a certain order.

Therefore, in app.config, right after <configuration>, the first section MUST be:

<configSections>
    <!-- Section groups, and stuff like userSettings, etc  -->
</configSections>

Followed by the

<connectionStrings>
    <add name="connectionString" connectionString="...whatever is here..."/>
</connectionStrings>

And then comes the rest.

In my case, I had:

<system.data>
  <DbProviderFactories>
    <remove invariant="System.Data.SQLite.EF6" />
    <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
  </DbProviderFactories>

As the very first element after <config&gt.

All I needed to was to move the <system.data> section and place it AFTER the <connectionStrings>, in order to respect the order that VS needs.

So in addition to needing to have the <configSections> before <connectionString> these must also be the two first sections right after <config&gt. This is what fixed it for me.

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