'EF Core 6 HasDefaultValueSql with multiple sequences

I'm currently re-writing an old service, and ran into an issue.

In the SQL Server we have a sequence for each clients product number. The old version used stored procedures for creating products. But I was looking into if it was possible to do something with HasDefaultValueSql.

The MS docs said to use .HasDefaultValueSql("NEXT VALUE FOR sequence"), but is there some way to differentiate which sequence to use based on the client_id column?

My product entity type configuration:

builder.ToTable("products");
builder.HasKey(e => e.Id);

builder.Property(e => e.Id).HasColumnName("product_id")
    .ValueGeneratedOnAdd();

builder.Property(e => e.ClientId).HasColumnName("client_id");
builder.Property(e => e.Number).HasColumnName("number")
    .HasDefaultValueSql("NEXT VALUE FOR client_idProductNumberSequence");

builder.Property(e => e.Name).HasColumnName("name");
builder.Property(e => e.Description).HasColumnName("description");
...other props

builder.HasIndex(e => new { e.ClientId, e.Number })
    .IsUnique(true);

If thats not possible, what would be the best way to solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source