How to configure PostgreSQL AutoIncrement using EF Core

0

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, type, increasing by 10 from 10.

What am I doing wrong? I would like it to start with 0 and autoincrement.

(
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");

    }
}
    
asked by anonymous 21.09.2018 / 23:49

2 answers

0

PostgreSQL is not the problem, but this class that ".ForNpgsqlUseSequenceHiLo ()"

  

When the interval is exhausted, a new interval is allocated. In   In practical terms, this uses a sequence that is incremented by some   value (100 by default), allowing the application to enter 100   lines in an autonomous way.

In other words if you have some type of timeout the ForNpgsqlUseSequenceHiLo will skip some boxes of the id and to insert.

I have generic project you can take a look and change your project, I'm using the framework but it's easy to change to the core, if you want: Postgres Generic Repository

    
09.10.2018 / 09:22
0

Following the Entity Framework Core documentation, you can use the following

.ValueGeneratedOnAdd();

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

For further information: link

    
09.10.2018 / 05:36