The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception

I’m working with EF5 in a MVC 4 aspnet website.
Locally, everything works just fine, but when I publish it to the IIS and try to enter, I get the error

“The type initializer for ‘System.Data.Entity.Internal.AppConfig’
threw an exception.”

Detailed exception

An error occurred creating the configuration section handler for
entityFramework: Configuration for DbContext type
GdpSoftware.Server.Data’ is specified multiple times in the
application configuration. Each context can only be configured once.
(E:Appweb.config line 104)

I checked previous question in StackOverflow, and I already uninstalled and reinstalled through Nuget EntityFramework and checked that each reference to it in each project is EF5. I also checked that the selected framework in each project is 4.5.

Any idea, What might be causing the issue?
Thanks! Guillermo.


<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please visit
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit --></configSections>
    <add name="GdpSoftwareConnectionString" connectionString="Persist Security Info=False;User ID=user;Password=password;Initial Catalog=databasename;Data Source=server" providerName="System.Data.SqlClient" />
    <add name="GdpSoftware.Server.Data.GdpSoftwareDbContext" connectionString="GdpSoftware.Server.Data.GdpSoftwareDbContext_ConnectionString" providerName="System.Data.SqlClient"/>
    <add name="GdpSoftware.Server.Ui.Web.Models.UsersContext" connectionString="GdpSoftware.Server.Ui.Web.Models.UsersContext_ConnectionString" providerName="System.Data.SqlClient"/>
    <add key="webpages:Version" value="" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
    <customErrors mode="Off" />
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true" />
    <validation validateIntegratedModeConfiguration="false" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <mimeMap fileExtension=".mustache" mimeType="text/plain" />
          <add fileExtension=".mustache" allowed="true" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Microsoft.Owin.Host.SystemWeb" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <context type="GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data" disableDatabaseInitialization="true">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data], [GdpSoftware.Server.Data.Migrations.Configuration, GdpSoftware.Server.Data]], EntityFramework" />


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Do the following in the App.config file,

  1. Put the connectionStrings element is after the configSections element.
  2. Put the startup element after the connectionStrings element.
<?xml version="1.0" encoding="utf-8"?>
         <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
         <add name="SchedulingContext" connectionString="Data Source=XXXSQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX"   providerName="System.Data.SqlClient"/>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

Method 2

I needed to change by defaultConnectionFactory to be SqlConnectionFactory instead of the default

  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameter value="<My Connection String>" />

Method 3

I Found that removing the references to Entity Framework and installing the latest version of Entity Framework from NuGet fixed the issue. It recreates all the required entries for you during install.

Method 4

I have broken my head over this issue, and finally here’s what worked for me:-

Step1 : Uninstall Entity framework using Nuget package manager

Step2: Delete Entityframework element from App.config

Step3: Reinstall desired version of Entity Framework.

Step4: delete Migrations table and Migrations folder.

Step5: Enable Migrations and Add Migration and Update database

Method 5

I had multiple providers specified in my web.config.

      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>

I simply removed one of those and it worked.

I am using MySQL though, not TSQL

Method 6

I also faced the same issue, however in my case my solution has a console application and EF class library which mainly interacts with database. I removed Config settings related to EF from the Console Application Config. I maintained the following configuration settings in EF class library i.e only at one place.

This worked for me.

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.2.61023.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

<add name="EntityModel" connectionString="Server=LocalhostSQLEXPRESS;Database=SampleEntities;Trusted_Connection=True;" providerName="System.Data.EntityClient" />

Method 7

I ran into this issue when I forgot to set my Connections.config file to “copy always”

BareMessage = “Unable to open configSource file ‘Connections.config’.”

Method 8

Change this part in your Web.config, according to the below code.(try this, it works to me.)

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />   

Method 9

in my case

<section name="entityFramework"

must be updated from version 4 to 6. I mean a project was updated EntityFramework from
4 to 6 but web.config was not updated.

Method 10

I think the problem is from this line:

<context type="GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data" disableDatabaseInitialization="true">

I don’t know why you are using this approach and how it works…

Maybe it’s better to try to get it out from web.config and go another way

Method 11

If you are maintaining one more cinfiguration file for app.config , Don’t include any key in parent page.

Parent Page : app.config

<appSettings configSource="appSettings.config">
    <add key="ClientSettings" value="venice" /> <!-- Don't add Key Here -->

Child Page : appSettings.config
  <add key="ClientSettings" value="venice"/> <!-- add Here -->

Method 12

I had same problem. After one day, I got it.

Problem was from adding two smtp tags in mailSettings under <>.

Method 13

Search in your web.config or App.Config if you have some tags that are not used in your project or you don’t have in your references.

Method 14

I had this error today on a nested MVC application running as virtual folder in onother MVC application. In my case the InnerException was more informative than the main one. It was stating:

- The entry 'DbContextMain' has already been added. (C:inetpub...web.config line x)

After fixing the duplicate connection strings in the nested apps everything worked fine.

Method 15

If you use ASP.NET and IISExpress go to “C:Users\DocumentsIISExpressconfigapplicationhost.config”, search for your Project and look if you have a faulty virtualDirectory entry.

Method 16

I faced similar issue and changing defaultConnectionFactory to be SqlConnectionFactory helped me solve it.

Method 17

actual error thrown Message=Unrecognized element ‘providers’ in web.config
so from the web.config file remove the providers section

Method 18

The general issue is just any issue involving Machine/Web/App configs.

I had the same connection strings in Machine.Config as in my App.Config so I put before my first connection string in my App.Config

Method 19

This weird error happens, when you play around with different versions of EntityFramework versions in Nuget Packages like I did.

First, Uninstall your Entity Framework DLL from NuGet packages and then Clean up app.config. By removing the entry from configSections and entity framework element.

Next, install the desired version. This should fix the problem.

Method 20

In connection string, the first string is the base in web.config

SchedulingContext is the base parameter of Entity file.

     <add name="SchedulingContext" connectionString="Data Source=XXXSQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX"   providerName="System.Data.SqlClient"/>

Method 21

in my case adding <clear /> just after <connectionStrings> worked like charm

Method 22

I had the duplicit definition of connection string in my WCF service. I was able to debug the service and to see the inner error message (not displayed by default):

ConfigurationErrorsException: The entry 'xxxEntities' has 
already been added. (C:UsersWcfServiceweb.config line 35).

which was after web.config transformation (note duplicit values)
    <add name="xxxEntities" connectionString="metadata=res://*/ ...
    <add name="xxxEntities" connectionString="metadata=res://*/ ...

Hence removing unwanted connection string solved my problem.

Method 23

I solved this problem removing DbproviderFactory in the section of the file machine.config, there was some dirty data when I installed fbclient.dll.


    <!-- <DbProviderFactories><add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient, Version=, Culture=neutral, PublicKeyToken=3750abcc3150b00c"/><add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient, Version=, Culture=neutral, PublicKeyToken=3750abcc3150b00c"/> -->

Method 24

Old question but it just happened to me for different reasons mentioned in the other answers, so I thought I’d share my findings.

In my case, the problem was how I defined my connection string entry in the app.config. I edited it directly in Notepad++ and I must have hit a shortcut, and minimized the entire string which I didn’t spot, and started to get this error. This is how I was defined it:

<add name="MyDb" connectionstring="data source=MyServer;
 initial catalog=MyDatabase;integrated security=true" 
 providername="System.Data.SqlClient" />

The second I changed the following parameters
connectionstring to connectionString

providername to providerName

Note: The problem was highlighted immediately In Visual Studio but clearly this isn’t ideal if you’re on a client site!

Method 25

I had the duplicate definition of connection string in my Project Cms.And the Context class is named:CmsContext

In my case, the problem was solved, as I changed the connectionsting in Web.config as follow:in first one name is CmsContext and it’s related to main project .in second one name is DefaultConnection and it’s related to Identity

<add name="CmsContext" providerName="System.Data.SqlClient" connectionString="Data Source=DESKTOP-2NQSP1PSQLEXPRESS;  Initial Catalog=CmsDB;Integrated Security=True;" />

Method 26

Step 1:uninstall-package entity framework: This Command Uninstall Existing Entitymanger in The Project

Step 2:uninstall-package entity framework: This Command Install Latest Version Into Project

At Last ReBuild The Project It Will Works Good

Method 27

Check the C-drive inside C:inetpubwwwroot and remove all unnecessary folders from it, else remove unnecessary host files or folders from IIS.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x