Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Error: The 'DbProviderFactories' section can only appear once per config file (IBM Client Access)

I'm using for my application Entity Framework and System.Data.SQLite using WPF and C# on .NET 4.5 On my machine al work well, but on a test machine a received this error when I access to the sqlite db via EntityFramework:

The 'DbProviderFactories' section can only appear once per config file.

I found that the error was that in the test machine, in the machine.config:

<system.data>
    <DbProviderFactories>
        <add name="IBM DB2 for i5/OS .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for i5/OS" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26"/>
        <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
    </DbProviderFactories>
    <!-- This is the line to remove - empty element --> 
    <DbProviderFactories/>
</system.data>

I remove the last empty element and now all work. I think it's something related the IBM.Data.DB2.iSeries installation (IBM Client Access).

My question is: How to remove the empty element without manually edit machine.config? I have tried to insert the tag <clear /> in the app.config file but not work.

I found another question similar to mine but not one suggest how to solve the problem without edit manually the machine.config to solve the problem.

this is my app.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.xml.serialization>
    <xmlSerializer useLegacySerializerGeneration="true" />
  </system.xml.serialization>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <!--STAI MOLTO ATTENTO-->
    <legacyUnhandledExceptionPolicy enabled="1"/>
  </startup>
  <connectionStrings>
    <add name="db" connectionString="Data Source=clients.db;Version=3;New=False;Compress=True;" />
    <add name="icmdbEntities" connectionString="metadata=res://*/MainModel.csdl|res://*/MainModel.ssdl|res://*/MainModel.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=.\icmdb.db&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <clear />
      <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>
  <appSettings>
    <add key="ditta" value="default" />
    <add key="demo" value="true"/>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>
like image 990
abrfra Avatar asked May 20 '14 07:05

abrfra


2 Answers

The problem is caused by installing IBM iAccess for Windows -- specifically the .NET Provider for DB2 component. I've seen it firsthand in V7R1, but others have referenced the same issue with V6R1.

IBM is aware of the problem and has a fix in one of the service releases.

From the V7R1 service release documentation:

DESCRIPTION OF PROBLEM FIXED FOR APAR SE45767 :

Under unknown circumstances, corruption to the machine.config XML file is occurring when the .Net data provider is installed (either as part of a Complete or Custom install type). The corruption is isolated to portions of the XML data related to the DbProviderFactories - and generally has been observed to include duplication of some lines of the XML data.

CORRECTION FOR APAR SE45767 :

A preventive fix will be provided which will eliminate the likely cause of the machine.config corruption.

A corrective fix to update already corrupted machine.config files will not be provided. Utilize the documented local fix or circumvention if possible.

CIRCUMVENTION FOR APAR SE45767 :

If the .Net data provider is not needed, this problem may be avoided by performing a custom install and ensuring the .Net Data provider will not be installed. If the .Net provider is required, no circumvention is known. Utilize the local fix to resolve the issue.

As for fixing the problem once it has occurred, you need to fix machine.config (both 32-bit and 64-bit), because it does not conform to the schema defined for system.data. That's easy -- just write a simple .NET app that uses XmlDocument to load machine.config, locate the duplicate DbProviderFactories element, delete it and save the file. Or use a PowerShell script or anything else that can manipulate XML documents.

Here's an IBM APAR discussing this issue in V6R1.

like image 55
Thomas F. Abraham Avatar answered Oct 04 '22 17:10

Thomas F. Abraham


I experienced this problem today.

The machine.config files are located in:

  • C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
  • C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine.config
  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config

The corrupted version of machine.config contains:

<system.data>
   <DbProviderFactories>
      <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
   </DbProviderFactories>
   <DbProviderFactories/>
</system.data>

Installing the iSeries drivers corrupted the following machine.config files:

| Framework | Platform | Factory Added? | Corrupted Machine.config? |
|-----------|----------|----------------|---------------------------|
|v4.0.30319 |   x64    |     Yes        |   Yes                     |
|v4.0.30319 |   x86    |     Yes        |   Yes                     |
|v2.0.50727 |   x86    |     No         |   No                      |
|v2.0.50727 |   x64    |     No         |   No                      |

It's nice that you only have to worry about IBM corrupting half your machine.config files; and of those only applications that use .NET 4.

Files to fix

  • C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config (ok)
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config (ok)
  • C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine.config (corrupt)
  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config (corrupt)

From an elevated Notepad, open the two files, and remove the extraneous empty <DbProviderFactories/> element, leaving you with:

<system.data>
   <DbProviderFactories>
      <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
   </DbProviderFactories>
</system.data>

And you're fixed.

like image 45
Ian Boyd Avatar answered Oct 04 '22 17:10

Ian Boyd