Solucionado (ver solução)
Solucionado
(ver solução)
5
respostas

Erro no Add-Migration

Não estou conseguindo efetuar o add-migration pois o mesmo apresenta erro:

The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception.

Alguém sabe como solucionar este erro?

5 respostas

Olá, Washington!

Parece que está faltando um pedaço do texto da exception. Você pode colar aqui a exception inteira, para podermos analisar melhor?

Sim claro. Exception inteira abaixo:

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: Unrecognized configuration section connectionString. (C:\Users\washington.souza\Documents\Visual Studio 2017\Projects\Financeiro\Financeiro\tmp3CF0.tmp line 47) at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal) at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors) at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()

at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)

End of inner exception stack trace --- at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.get_ConnectionStrings() at System.Data.Entity.Internal.AppConfig..ctor()

at System.Data.Entity.Internal.AppConfig..cctor()

End of inner exception stack trace --- at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForAssembly(Assembly assemblyHint, Type contextTypeHint) at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForContext(Type contextType)

at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..cctor()

End of inner exception stack trace --- at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..ctor()

at Financeiro.Migrations.Configuration..ctor() in C:\Users\washington.souza\Documents\Visual Studio 2017\Projects\Financeiro\Financeiro\Migrations\Configuration.cs:line 10

End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Data.Entity.Migrations.Utilities.MigrationsConfigurationFinder.FindMigrationsConfiguration(Type contextType, String configurationTypeName, Func2 noType, Func3 multipleTypes, Func3 noTypeWithName, Func3 multipleTypesWithName) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration() at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges) at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges) at System.Data.Entity.Migrations.AddMigrationCommand.<>cDisplayClass2.<.ctor>b0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception.

Classe Configuration:

namespace Financeiro.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Financeiro.DAO.FinancasContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(Financeiro.DAO.FinancasContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}
solução!

Obrigado!

O sistema de configuração não encontrou o tipo connectionStrings. Veja se seu web.config contém essa linha:

 <configSections>
.
.
.
    <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
.
.
.
 <configSections>

Mas olhando novamente, é possível que você tenha <section name="connectionString" > (com connectionString no singular) em vez de <section name="connectionStrings" > (com connectionStrings no plural). Nesse caso você tem que mudar o valor de name em section.

Espero que ajude!

Isso mesmo, funcionou.

Obrigado