开发者

Error when adding a configuration to App.config file

开发者 https://www.devze.com 2023-01-26 03:12 出处:网络
Related question: Running my application on another machine gives me an error This is how my App.config file looks like:

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.


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>


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号