Error when adding a configuration to App.config file
Asked Answered
C

3

3

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.

Coreencorel answered 19/11, 2010 at 14:25 Comment(0)
G
2

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

Genova answered 19/11, 2010 at 14:29 Comment(3)
Yeah, that's what I'm thinking. How can I change the path to something dynamic?Coreencorel
@Serge: This isn't what is giving you the error but is something you should consider doing. Change your data source in you connection string to be a relative path like data source=&quot;.\DocumentsDB.sqlite&quot; and place the sqllite database in your application root. Or if it needs to be in the users folder you could have source=&quot;|DataDirectory|\DocumentsDB.sqlite&quot;Surfacetoair
If you want to change it at runtime, you can use the RedirectedEntityFrameworkConnectionString method from this blog post.Intertexture
S
11

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>
Surfacetoair answered 19/11, 2010 at 14:30 Comment(5)
When I add that to my config, I get this error: "Failed to find or load the registered .Net Framework Data Provider.". If I take that out, the application works well on my target machine.Coreencorel
@Serg Did you notice the <Clear /> before the addSurfacetoair
What about it? I've pasted in the exact configuration you posted :( What does the clear do?Coreencorel
@Serg Clear should clear the ones registered in the machine.config. Try my update.Surfacetoair
@Serg I just saw your error message you posted when you use clear "Failed to find or load the registered .Net Framework Data Provider." How are you deploying SqlLite to the target machine. Is it in the bin folder of your application? Did you set the reference to Copy Local?Surfacetoair
O
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.

Ofeliaofella answered 16/3, 2013 at 11:30 Comment(0)
G
2

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

Genova answered 19/11, 2010 at 14:29 Comment(3)
Yeah, that's what I'm thinking. How can I change the path to something dynamic?Coreencorel
@Serge: This isn't what is giving you the error but is something you should consider doing. Change your data source in you connection string to be a relative path like data source=&quot;.\DocumentsDB.sqlite&quot; and place the sqllite database in your application root. Or if it needs to be in the users folder you could have source=&quot;|DataDirectory|\DocumentsDB.sqlite&quot;Surfacetoair
If you want to change it at runtime, you can use the RedirectedEntityFrameworkConnectionString method from this blog post.Intertexture

© 2022 - 2024 — McMap. All rights reserved.