Remove serial column creation Entityframework + Npgsql

Asked

Viewed 70 times

2

Ola, I am creating an application using Fluent API + Entityframework. But in my domain classes when determining that a property is PK it automatically defines it as serial, I would not like to get this behavior because it will automatically create a SEQUENCE for each PK field of my tables. How do I remove this validation and define that when the field is a primary key I do not want it to be serial but only integer?

Thank you

  • Although I answered the question for you (if it’s the right one, mark it as correct), I would like to know what your scenario is, or concern about not using IDENTITY ("serial") for your Keys. This is usually indicated.

1 answer

1

You need to mark the column to not generate the sequence. This should solve for you:

modelBuilder
    .Entity<*nome_da_entidade*>()
        .Property(m => m.*campo*)
             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

Another way I imagine to be automatic would be to include the attribute below directly in the property:

[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(
    System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]

Or, rather, add System.ComponenteModel.DataAnnotations.Schema to its clauses using and just like that:

[DatabaseGenerated(DatabaseGeneratedOption.None)]

Browser other questions tagged

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