Code First can't enable migrations

2020-06-01 07:04发布

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?

11条回答
做自己的国王
2楼-- · 2020-06-01 07:38

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

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    ...
  </configSections>
  <appSettings>
   ...
  </appSettings>
  ...
<configuration>
查看更多
干净又极端
3楼-- · 2020-06-01 07:53

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" />
查看更多
疯言疯语
4楼-- · 2020-06-01 07:58

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!

查看更多
女痞
5楼-- · 2020-06-01 07:59

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.
查看更多
ら.Afraid
6楼-- · 2020-06-01 07:59

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.

查看更多
对你真心纯属浪费
7楼-- · 2020-06-01 08:00

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.

查看更多
登录 后发表回答