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

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>



Solution 1:[1]

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

<configuration> 

... ...

Solution 2:[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:[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:[4]

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>

Solution 5:[5]

Even I got this error while migrating. I had a data access layer(DAL) having my context class and web application referencing the data access layer. To migrate you have to add the connection string in the app.congig file of the DAL and also the web.config file in your web application. On removing the default connection string from the web.config file it worked for me. Replace this

<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-foo-20190108014329.mdf;Initial Catalog=aspnet-foo-20190138014529;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

by

<connectionStrings> <add name="foo" connectionString="Data Source=foo; Initial Catalog=foo; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/> </connectionStrings>

This worked for me.

Solution 6:[6]

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 7:[7]

This configuration worked for me:

<?xml version="1.0" encoding="utf-8"?>
<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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <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.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MyLocalDatabase" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********" />
  </connectionStrings>
</configuration>

I was getting errors similar to yours because of a wrong installation of the MySql connector client in my development machine. I installed MySql connector Nuget packages within Visual Studio, but it was not enough. I also needed to install MySql client which I downloaded from MySql website. After that the Type errors went away.

These are my Nuget packages: enter image description here

And this is the download page of MySql connector which I downloaded and installed manually.

Note: the defaultConnectionFactory value is never used if you specify explicitly the type of your provider. In that case it doesn't matter which value you have in the configuration file in this setting. See point 3 in this reference doc page in MySql website.

I recommend you to check if your MySql is correctly installed in your machine by using MySql command line client console. Try to connect to the server from outside Visual Studio with your connection string values.

Solution 8:[8]

Remove the connection string and add the migration again. Connection string will be added automatically. I am having the same issue and it worked for me.

Solution 9:[9]

When I had this problem I realised that entityframework was not declared in config file.

So I added

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

above the connection string

Solution 10:[10]

<connectionStrings> 
  <add name="DefaultConnection" connectionString="Server=DESKTOP-ABHI;Initial Catalog=IdentityDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

First check the connection string in the web.config file.

If you have two connection strings then comment the second connection string.

Solution 11:[11]

I had these same issue... Check Root Web.config (Fixed)

  • 1- Check the configSections is first child
  • 2 Remove the Extra connection strings you have (Comment them out) Stay with the one you want.

Note: I was reading leeNaylors book(asp.net mvc with entity f...) according to his book this should´not be an issue, having more than 1 Connection string... but it turns out it does matter for migrations. [web.config][image]

Thumbs up if my answered helped you...

Web.config image