How to configure the Incrementby of a Postgre Id field using the Entity Framework Core

Asked

Viewed 37 times

0

I have a table mapped for Postgre, but when I update the database, the Personal Fieldphysicaenderecotipoid is being auto-incremented by 10 in 10 (by default). How do I map so that Incrementby is set to auto-increment 1 in 1?

inserir a descrição da imagem aqui

Mapping of the table:

public class PessoaFisicaEnderecoTipoMap : IEntityTypeConfiguration<PessoaFisicaEnderecoTipo>
{
    public void Configure(EntityTypeBuilder<PessoaFisicaEnderecoTipo> builder)
    {

        builder.ToTable("PessoaFisicaEnderecoTipo");

        builder.HasKey(pfet => pfet.Id);

        builder.Property(pfet => pfet.Id)
            .HasColumnName("PessoaFisicaEnderecoTipoId")
            .ForNpgsqlUseSequenceHiLo()
            .IsRequired();

        builder.Property(pfet => pfet.Descricao)
           .HasColumnName("Descricao")
           .HasColumnType("character varying(50)")
           .IsRequired();

        builder.Property(pfet => pfet.PadraoSistema)
          .HasColumnName("PadraoSistema")
          .HasColumnType("boolean");

    }


}
  • https://www.npgsql.org/efcore/value-generation.html and an explanation here: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/336 read and then speak to me!

1 answer

1


The 10 in 10 sequence is the default when you are using ForNpgsqlUseSequenceHiLo. You can create your custom sequence 1 in 1 and report as below:

modelBuilder.HasSequence("minha_sequencia", b => b.IncrementsBy(1))    

builder.Property(pfet => pfet.Id)
    .HasColumnName("PessoaFisicaEnderecoTipoId")
    .ForNpgsqlUseSequenceHiLo("minha_sequencia")
    .IsRequired();
  • 1

    Thanks @Fabri Damazio. It worked 100!

Browser other questions tagged

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