How to configure Postgresql Autoincrement using EF Core

Asked

Viewed 350 times

1

I have a table mapping (Entity Framework Core 2.0) in which the Id field needs to be Autoincrement.

The problem is that Postgresql is starting with high value and often jumps to very high sequences, like, increasing from 10 to 10.

What am I doing wrong? I would like it to be started with 0 and be self-administered.

(
public class ProfissaoMap : IEntityTypeConfiguration<Profissao>
{
    public void Configure(EntityTypeBuilder<Profissao> builder)
    {
        builder.ToTable("Profissao");

        builder.HasKey(p => new {p.Id});

        builder.Property(p => p.Id)
            .ForNpgsqlUseSequenceHiLo()
            .IsRequired();

        builder.Property(p => p.Id)
            .HasColumnName("ProfissaoId")
            .HasColumnType("integer")
            .IsRequired();

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

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

    }
}
  • Why do you have this builder.HasKey(p => new {p.Id});? Shouldn’t be builder.HasKey(p => p.Id);? It must have no relation to the problem described, but I was surprised.

  • I once followed an example on the net that taught how to use new... It works both ways @hkotsubo rsrsssrs

2 answers

0

0


Postgresql is not the problem, but this class that ". Fornpgsqlusesequencehilo()"

When the interval is exhausted, a new interval is allocated. In practical terms, this uses a sequence that is incremented by some large value (100 by default), allowing the app to enter 100 lines autonomously.

in other words if you have some kind of timeout the Fornpgsqlusesequencehilo will skip some id boxes and to insert.

I have project Enerico vc can take a look and change your project, I am using the framework but and easy to change to the core if you want: Postgres Generic Repository

  • Thank you @Hudsonph! :)

Browser other questions tagged

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