The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception

21,169

Solution 1

Have fixed it by making the configSections the first child of configuration in the config file.

<configuration> 

... ...

Solution 2

In Entity Framework The type initializer error is most likely due to malformed Web.config files, like having two or more sections of connection string, or having problem in the connection string itself, such as invalid provider.

Solution 3

Just add configSections inside the configuration section if not added. And configSections must be first child of configuration section just like below code.

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="ConnectionString"
    connectionString="data source=20.20.1.1;initial catalog=myDB;user id=sa;password=123456;persist security info=True;MultipleActiveResultSets=True;App=EntityFramework"
    providerName="System.Data.SqlClient"/>
  </connectionStrings>
....
<!-- other configuration -->
....
</configuration>

Solution 4

I found a solution to this problem.

My app.config was missing a section name in configSections:

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

I simply added it and it worked.

Solution 5

The type initialization error arises because the invariantName=MySql.Data.MySqlClient appears twice in the <providers> section of the web.config file.

Removing one of them (by commenting it out) solves the issue:

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <!--<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
  </providers>
</entityFramework>
Share:
21,169
Kgn-web
Author by

Kgn-web

Web Application Engineer

Updated on December 14, 2020

Comments

  • Kgn-web
    Kgn-web over 3 years

    I have ASP.Net MVC site.

    Technology Stack

    • ASP.Net 4.6
    • C#.Net
    • EF 6
    • MySQL -Database

    While I am trying to generate the database using Nuget command:-

    Enable-Migrations -force
    

    I am getting the below exception

    The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception.

    Following things are already cross checked & tried by me:-

    My App.Config:-

     <connectionStrings>
    <add name="mydbContext" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;port=8080;database=mydb;uid=root;password=" />
    </connectionStrings>
    
    
    
    <entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory,   MySql.Data.Entity.EF6" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>