Code First can't enable migrations

2020-06-01 07:25发布

问题:

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?

回答1:

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.



回答2:

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.


回答3:

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!



回答4:

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.



回答5:

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.



回答6:

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" />


回答7:

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.



回答8:

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



回答9:

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.



回答10:

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

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    ...
  </configSections>
  <appSettings>
   ...
  </appSettings>
  ...
<configuration>


回答11:

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.