I'm looking for best practices in order to increment some numbers in my DB using EF Core..
I have a DB model called PARENT. In my application, I can have several PARENTS. Each PARENT has several CHILDS.
PARENT1: CHILD1 - CHILD2
PARENT2: CHILD1 - CHILD2 - CHILD3
I want, inside each PARENT, each children be numbered incrementally by a column: int Number
. This means,
PARENT1.CHILD1 = 001;
PARENT1.CHILD2 = 002;
PARENT2.CHILD1 = 001;
PARENT2.CHILD2 = 002;
...
So, it has to be only unique and incremented by parent..
Can anyone give me some best practices in order to achieve this in an efficient way?
UPDATE:
public class Bar {
public Guid BarId {get;set;}
public ICollection<Client> Clients {get;set;}
}
public class Client {
public Guid ClientId {get;set;}
public Guid BarId {get;set;}
public int ClientNumber {get;set;}
}
public class Context : DBContext {
entity.HasKey(e => new { e.BarId, e.ClientId }).HasName("PK_CLIENT");
entity.Property(e => e.ClientId).ValueGeneratedWhenDefault();
entity.Property(e => e.ClientNumber).ValueGeneratedOnAdd();
entity.HasIndex(e => new {e.BarId, e.ClientNumber}).IsUnique()
.HasName("IX_CLIENT_NUMBER");
}
By calling ValueGeneratedOnAdd()
I make the number column increment, but I want it to increment for each Bar, so it should allow duplicated client numbers between different bars, but not at the same Bar, and also increment them by Bar, not just by the Client entity.