Map a table and enable Auto-increment of a FK field in Postgre using EF Core and Fluent Api

Asked

Viewed 118 times

1

I am unable to activate the Auto_increment of the Contactotype field in Postgre. In the table mapping I specified the "Valuegeneratedonadd" resource, but when I update the database with Migrations, nothing changes. When I try to enter a record in the Application it gives error because the field is not being incremented. Does anyone know how to help me?

A hug to all!

public class Contatotipomap : Ientitytypeconfiguration { public void Configure(Entitytypebuilder Builder) { Builder.Totable("Contact type");

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

        builder.Property(ct => ct.Id)
             .ValueGeneratedOnAdd()
            .IsRequired();

        builder.Property(ct => ct.Id)
            .HasColumnName("ContatoTipoId")
            .HasColumnType("integer")
            .IsRequired();

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

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

    }
}

inserir a descrição da imagem aqui

1 answer

0


I have similar cases that work auto increment, but in my tables the primary keys are of type Guid (UUID).

Researching about your problem the Npgsql in Value Genaration asks to enable the option ForNpgsqlUseIdentityColumns():

protected override void OnModelCreating(ModelBuilder modelBuilder)
    => modelBuilder.ForNpgsqlUseIdentityColumns();

Remembering that Postgresql calls its auto increment Serial. In the link you find the documentation very well made for all these questions.

I hope I’ve helped.

Browser other questions tagged

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