Auto-increment composite key that is partitioned by a foreign key in EF Core

27 views Asked by At

I have a table with a composite key of Comment_Id and Prod_Id. Prod_Id is the FK to another table, and I'd like to auto-increment the Comment_Id, but I want it to be partitioned by Prod_Id, so it should restart the count with every new Prod_Id.

Similar to this table:

Table Layout

Currently, I have this in my DbContext file:

builder.Entity<Comments>()
       .HasKey(x => new { x.CommentId, x.ProdId });
builder.Entity<Comments>()
       .Property(p => p.CommentId).ValueGeneratedOnAdd();

This doesn't work, since it won't reset the auto-increment with every Prod_Id.

0

There are 0 answers