Auto-increment on partial primary key with Entity Framework Core

后端 未结 5 2050
野趣味
野趣味 2020-12-13 05:50

I have declared the following model using the EF Core fluent API:

modelBuilder.Entity()
    .HasKey(p => new { p.Name, p.Id });
5条回答
  •  -上瘾入骨i
    2020-12-13 06:17

    Well those Data Annotations should do the trick, maybe is something related with the PostgreSQL Provider.

    From EF Core documentation:

    Depending on the database provider being used, values may be generated client side by EF or in the database. If the value is generated by the database, then EF may assign a temporary value when you add the entity to the context. This temporary value will then be replaced by the database generated value during SaveChanges.

    You could also try with this Fluent Api configuration:

    modelBuilder.Entity()
                .Property(f => f.Id)
                .ValueGeneratedOnAdd();
    

    But as I said earlier, I think this is something related with the DB provider. Try to add a new row to your DB and check later if was generated a value to the Id column.

提交回复
热议问题