The type of entity requires a primary key to be defined

Asked

Viewed 22 times

0

I am creating a web api using the .net 5

The purpose of the application is to make a simple user registration, and in the future other types of registration

I’m using an architecture DDD, and in my repository layer I use the Entity Framework Core Version 5.0.8 with a connection to a bank SQL Server

This is my Context class with the bank Applicationdbcontext

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    public virtual DbSet<Pessoa> Pessoa { get; set; }
    public virtual DbSet<Licenca> Licenca { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PessoaConfig>();
        modelBuilder.Entity<LicencaConfig>();


        base.OnModelCreating(modelBuilder);
    }
}

I have a configuration class of each entity, I will use example the class Person which is where I’m doing the first tests of my application

Configuration class of the Person

public class PessoaConfig : IEntityTypeConfiguration<Pessoa>
{
    public void Configure(EntityTypeBuilder<Pessoa> builder)
    {
        builder.ToTable("Cliente");

        builder.HasKey(x => x.CodigoCliente);

        builder.Property(x => x.CodigoCliente).HasColumnName("cd_cliente");
        builder.Property(x => x.TipoPessoa).HasColumnName("tp_pessoa");
        builder.Property(x => x.Advogado).HasColumnName("fl_advogado");
        builder.Property(x => x.Captador).HasColumnName("fl_captador");
        builder.Property(x => x.Cobrador).HasColumnName("fl_cobrador");
        builder.Property(x => x.Cliente).HasColumnName("fl_cliente");
        builder.Property(x => x.Fornecedor).HasColumnName("fl_fornecedor");
        builder.Property(x => x.Devedor).HasColumnName("fl_devedor");
        builder.Property(x => x.Nome).HasColumnName("no_cliente");
    }
}

My problem is the sequinte, in my Repositorio class, when I will create the person in my bank it bursts an error.

My Method in the Repository

public class PessoaRepository : IPessoaRepository
{
    protected readonly ApplicationDbContext _context;
    private readonly IErrorMenssageService _svcError;

    public PessoaRepository(ApplicationDbContext context, IErrorMenssageService svcError)
    {
        _context = context;
        _svcError = svcError;
    }

    public void Criar(Pessoa obj)
    {
        try
        {
            _context.Pessoa.Add(obj); //Meu erro estoura nessa linha
            _context.SaveChanges();
        }
        catch (Exception ex)
        {
            _svcError.InserirMensagem(ex.Message);
        }
    }
}

Bug Name

The entity type 'Pessoa' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.

I have done clotheslines searches to try to solve, and they all tell me to have in the configuration class this code snippet (which is in the code above)

builder.HasKey(x => x.CodigoCliente);

But that doesn’t solve my problem

1 answer

1


you need to instantiate your configuration class as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfiguration(new PessoaConfig());
    modelBuilder.ApplyConfiguration(new LicencaConfig());


    base.OnModelCreating(modelBuilder);
}
  • 1

    Thank you so much for your help !!!

Browser other questions tagged

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