Making Codefirst Error in Enityframework

Asked

Viewed 1,684 times

0

That’s the mistake that’s happening:

An unhandled Exception of type 'System.Typeinitializationexception' occurred in Entityframework.dll Additional information: O 'System.Data.Entity.Internal.Appconfig' type initializer triggered 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; }           

    }
}

1 answer

2


The configSections TAG is in the wrong place, it should be unique and be the first in the settings file, IE, have to move it to before the connectionStrings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.