Error when adding a configuration to App.config file

穿精又带淫゛_ 提交于 2019-12-07 16:08:23

问题


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.


回答1:


The problem is most likely that you hard code a path to your desktop in your connection string:

C:\Users\Sergio.Tapia\Desktop\DocumentScannerDanyly\DocumentScannerDanyly\DocumentsDB.sql

Unless this file is present with the exact same location on the other machine, chances iare it won't work




回答2:


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>



回答3:


I had the same exact problem, when adding the above solution I got

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

but what solved it was to set "Copy Local" to true for System.Data.SQLite and for System.Data.SQLite.Linq in the References.

I hope it'll help you too.



来源:https://stackoverflow.com/questions/4225908/error-when-adding-a-configuration-to-app-config-file

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