How to tell Entity Framework that my ID column is auto-incremented (AspNet Core 2.0 + PostgreSQL)?

前端 未结 3 2106
生来不讨喜
生来不讨喜 2021-02-07 06:52

Code is simple. Tag.cs entity:

public partial class Tag
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { ge         


        
3条回答
  •  遥遥无期
    2021-02-07 07:13

    You have to use here "ValueGenerationOnAdd()". As the issue you are getting is already reported on GitHub. Please find the below link.

    https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/73

    You can find more info regarding Generated Value pattern from following link.

    Value generated on add

    public classs SampleContext:DBContext{
    public DbSet Tag { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder){
        modelBuilder.Entity()
            .Property(p => p.ID)
            .ValueGeneratedOnAdd();
     }
    public class Tag{
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description{get;set;}
      }
    }
    

    Source:- https://www.learnentityframeworkcore.com/configuration/fluent-api/valuegeneratedonadd-method

    Hope this will help

提交回复
热议问题