Error Making CodeFirst in EnityFramework

0

This is the error that is occurring:

  An unhandled exception of type 'System.TypeInitializationException'   occurred in EntityFramework.dll Additional information: O   initializer of type 'System.Data.Entity.Internal.AppConfig'   has thrown an exception.

This is my App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ProdutoDb" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ProdutoDb;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

And this is my context:

using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;    

namespace Data
{
    public class ProdutoDbContext : DbContext
    {
        public ProdutoDbContext() : base("name=ProdutoDb")
        {
            //Verifica se a base de Dados existe, e se não existir ele cria.
           Database.SetInitializer<ProdutoDbContext>(new CreateDatabaseIfNotExists<ProdutoDbContext>());
           Database.Initialize(false);
        }           

        public DbSet<Produto> Produtos { get; set; }
        public DbSet<Loja> Lojas { get; set; }           

    }
}
    
asked by anonymous 15.11.2016 / 18:08

1 answer

2

The TAG configSections is in the wrong place, it must be unique and be the first one of the settings file, ie it has to move it to before the connectionStrings.

    
17.11.2016 / 16:42