C# – Error when adding a configuration to App.config file

cdbproviderfactoriesentity-frameworksqlite

Related question:
Running my application on another machine gives me an error

This is how my App.config file looks like:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DocumentsDBEntities" connectionString="metadata=res://*/Documents.csdl|res://*/Documents.ssdl|res://*/Documents.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sqlite&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
  </startup>
  <appSettings>
    <add key="Username" value="administrador"/>
    <add key="Password" value="123456"/>
  </appSettings>
</configuration>

Running this on my dev machine works, but when deploying to another computer, I get an Data Provider error. (see related question above).

The suggested solution was to add this to the App.config file:

<system.data>
        <DbProviderFactories>
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

When I add that to the App.config file, I get this error when launching the application in Visual Studio 2010:

An error occurred creating the
configuration section handler for
system.data: Column 'InvariantName' is
constrained to be unique. Value
'System.Data.SQLite' is already
present.
(C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\bin\Debug\DocumentScannerDanyly.vshost.exe.Config
line 13)

Any suggestion on what this error is? Also, since the location of the .sqlite file is relative to where it is installed, do I have to change the connectionString in the AppConfig file to something more dynamic?

Thanks for the help.

EDIT:

When I add this to the config as suggested by someone here, I get an error:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

Failed to find or load the registered
.Net Framework Data Provider.

Best Answer

This is because when you install SqlLite it updates your machine.config with:

<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

Because you are not running on a machine where SqlLite has been installed the DbProviderFactories does not know about SqlLite.

Either install SqlLite on your destination machine or add this to your config:

<system.data>
        <DbProviderFactories>
                <clear />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>

This will stop you clashing with your machine.config, and allow it work on your target machine. If you are using any other Sql data provider you would also need to add that as well.

EDIT

If clear isn't working try:

<system.data>
        <DbProviderFactories>
                <remove invariant="System.Data.SQLite" />
                <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
</system.data>