Error connecting to Entity Framework in an existing BD

Asked

Viewed 18 times

1

I created an application that uses Entity Framework 6X and Code First mode by Fluent API, Map Classes, using classes from my model , and creating the context class. When I try to connect to see if at least one of my entities returns some information and if the connection with the bank will occur correctly, he gives me the error message: "An exception occurred without treatment of type "System.Invalidoperationexception" in Entityframework.dll The store type 'Numeric(2, 0)' could not be found in the Sqlserver Provider manifest". in the debug in the Console application it goes through the first line of db definition, and the error occurs when trying to get a Tolist() from the Taxes entity.

Below is my context class, the connection string in the BD of the app.config, my model class, and the code called test in the console application.

Context :

namespace apiIntegracaoSFI.DAL.Modelo
{
   public class SFIContext : DbContext
    {
        public SFIContext() : base("SFIContext")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = true;
        }

        public DbSet<Imposto> Impostos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("dbo");

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Properties()
                .Where(p => p.Name == p.ReflectedType.Name + "Id")
                .Configure(p => p.IsKey());

            modelBuilder.Properties<string>()
                .Configure(p => p.HasColumnType("varchar"));

            modelBuilder.Properties<string>()
                .Configure(p => p.HasMaxLength(100));

            modelBuilder.Configurations.Add(new ImpostoMap());

        }
    }
}

app config. :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
  </startup>
  <connectionStrings>
    <add name="SFIContext"
         connectionString="data source=SQLNTE103;initial catalog=FATURAMENTO;user id=SFI;password=*********;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Mapping Class :

namespace apiIntegracaoSFI.DAL.DataAcess.Mapping
{
    public class ImpostoMap : EntityTypeConfiguration<Imposto>
    {
        public ImpostoMap()
        {

            this.ToTable("SAP_IMPOSTO");

            this.HasKey(e => new { e.CodigoSap, e.CategoriaImpostos });

            this.Property(e => e.CodigoSap)
                .HasColumnName("CODIGO_SAP")
                .HasColumnType("VARCHAR")
                .HasMaxLength(10);


            this.Property(e => e.CategoriaImpostos)
                .HasColumnName("CATEGORIA_IMPOSTOS")
                .HasColumnType("VARCHAR")
                .HasMaxLength(4);


            this.Property(e => e.ClassificacaoFiscal)
                .HasColumnName("CLASSIFICACAO_FISCAL")
                .HasColumnType("VARCHAR")
                .HasMaxLength(1);


            this.Property(e => e.Pais)
                .HasColumnName("PAIS")
                .HasColumnType("VARCHAR")
                .HasMaxLength(3);

        }
    }
}

Model Class :

namespace apiIntegracaoSFI.DAL.Modelo
{
    public class Imposto
    {
        public string CodigoSap { get; set; }
        public string CategoriaImpostos { get; set; }
        public string ClassificacaoFiscal { get; set; }
        public string Pais { get; set; }
    }
}

Console Test Code:

namespace ConsoleTeste
{
    public class Program
    {
        static void Main(string[] args)
        {

            SFIContext db = new SFIContext();

            var impostos = db.Impostos.ToList();

            foreach (var imp in impostos)
            {
                Console.WriteLine("Codigo SAP: " + imp.CodigoSap);
                Console.WriteLine("Imposto: " + imp.CategoriaImpostos);
                Console.WriteLine("Classificacao: " + imp.ClassificacaoFiscal);
                Console.WriteLine("Pais: " + imp.Pais);
            }
            Console.ReadKey();

        }
    }
}
No answers

Browser other questions tagged

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