Code First can't enable migrations

北战南征 提交于 2019-12-03 05:20:51

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.

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.

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!

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.

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.

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

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.

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

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.

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

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    ...
  </configSections>
  <appSettings>
   ...
  </appSettings>
  ...
<configuration>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!