14

I'm trying to enable migrations but it's throwing an exception:

Checking if the context targets an existing database... System.TypeInitializationException: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: The 'name' attribute must be specified on the 'section' tag.

I'm assuming that the App.config file is not correctly set up (it was automatically set up when I added EF package). All I did was add the connection string:

<?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" />
 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>

 <connectionStrings>
   <add Name="MyContext" connectionString="data source=MYSERVER;initial catalog=CodeFirstTest;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
 </connectionStrings>

 <entityFramework>
   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
     <parameters>
       <parameter value="v11.0" />
     </parameters>
   </defaultConnectionFactory>
   <providers>
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>
 </entityFramework>
</configuration>

I'm using SQL Server 2008 R2.

As I do have a connection string, I don't believe I need the defaultconnectionfactory. Am I correct? (Note: Even without this section I'm still getting the same exception)

What else am I missing?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

11 Answers11

52

I had the same issue when my <connectionString /> entry is on top part of the Web.config, Right after <configuration>. Then I tried moving it right before </configuration>, and it worked.

Lester S
  • 720
  • 7
  • 21
  • 7
    Just so future readers know, the first element after `` must be `` if there are any custom configuration sections defined. Entity Framework adds a custom configuration section named ``. Therefore, especially when using Entity Framework, you'll break your app if you put anything else right after `. – Zac Charles Jun 02 '16 at 13:30
  • For me, my `` was placed in the right place, but we had some build transformations going on. When the code was built by TFS after we deployed it it was using the wrong values for the connection string. – markdotnet Sep 27 '16 at 13:21
7

This is mainly to do with the config file. So the actual stack trace that helps is "System.Configuration.ConfigurationErrorsException". There can be many reasons but they all majorly include the correction in the config file as answered earlier. Couple of possibilities which are little different than this are given below (But the stack really tells us)

  1. One possible out of the way reason can be that, your project where the migrations are getting enabled can be different from the startup project. So be sure to add -StartUpProject to your nuget commmand.
  2. Version of the entity framework used can be different in case of two different projects.
skillworks
  • 332
  • 3
  • 10
  • 1
    Nice one, #1 was the problem. Thanks. – user1477388 Oct 11 '15 at 16:47
  • This error occurred for me when Web.config (or App.config in the original author's case) had some unintended characters in it. Check your version control diff and see if you've made any unwanted changes to that config file. – dave_k_smith Dec 16 '15 at 19:30
  • Simply right-clicking the desired project and setting as the startup worked for me. Thanks for this. – JustSomeDev Oct 19 '16 at 14:56
2

I met with the same issue. My problem is that there are double connection string on the Web.config file. like as below:

<add name="DB1234" ..../> <add name="DB1234" ..../> So we have to check our web.config file first! Good Luck!

nisiumi
  • 344
  • 3
  • 7
2

check the spelling of your 'connectionString' and make sure it is 'connectionStrings' I omitted the 's' before in my own case. After adding the 's', that solved it.

micheal__og
  • 69
  • 1
  • 6
1

In my case I had several projects in the solution and another project was set as the StartUp project. Setting the project that I was trying to enable migrations for as the StartUp project resolved it.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
1

In my case, using DevExpress MVC generated template, it need to add extra lines after <sectionGroup name="devExpress">...<sectionGroup/> in web.config

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

in my experiences, for example is that all the information that is used to establish a connection with the database is the one that comes from the config file that is in the startup-project, and not the one that is in the project where i am generating or using the enable-migrations or update-database instructions, example i have project PRINCIPAL and another project DATA,i use the DATA project as my default-project only when running the packages in Packager console Manager, but the config file that really use is the one in PRINCIPAL.

Abdiel
  • 1
0

Include this into <configSections>

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

In my case this line is omitted when I have 2 instances of Visual Studio open. Close all instances of the Visual Studio, open and reinstall EF via interface Nuget or Console to avoid this error

Davi Menezes
  • 507
  • 1
  • 5
  • 10
0

Check the webconfig, For example: I had the app settings like this:

   <configuration> 
    <appSettings>
          <add key="dhx_license" value="value"/>
    </appSetting>
.....

and threw that error. But then I realized appSetting was duplicated below so I moved the and the error vanished. Thanks.

ferralucho
  • 637
  • 7
  • 24
0

Like @Lester answer check web config. It must look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    ...
  </configSections>
  <appSettings>
   ...
  </appSettings>
  ...
<configuration>
Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
0

I had the same issue. But I found there are two <connectionString /> in my Web.config, I had to remove one connection string and it work fine for me.

Krishna Vyas
  • 1,009
  • 8
  • 25
Md. Asaduzzaman
  • 137
  • 1
  • 12