Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First can't enable migrations

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?

like image 733
Ivan-Mark Debono Avatar asked Nov 26 '14 10:11

Ivan-Mark Debono


People also ask

Why add-migration is not working?

add-migration : The term 'add-migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

How do I enable migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.

How do I enable migrations on my EF core?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.


2 Answers

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.

like image 73
Lester S Avatar answered Nov 15 '22 08:11

Lester S


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.
like image 21
skillworks Avatar answered Nov 15 '22 08:11

skillworks