title: Best practices for auto increment a number EF Core
body: <p>I'm looking for best practices in order to increment some numbers in my DB using EF Core..</p>
  
  <p>I have a DB model called PARENT. In my application, I can have several PARENTS. Each PARENT has several CHILDS.</p>
  
  <p>PARENT1: CHILD1 - CHILD2</p>
  
  <p>PARENT2: CHILD1 - CHILD2 - CHILD3</p>
  
  <p>I want, inside each PARENT, each children be numbered incrementally by a column: <code>int Number</code>. This means, </p>
  
  <p>PARENT1.CHILD1 = 001; </p>
  
  <p>PARENT1.CHILD2 = 002; </p>
  
  <p>PARENT2.CHILD1 = 001; </p>
  
  <p>PARENT2.CHILD2 = 002;</p>
  
  <p>... </p>
  
  <p>So, it has to be only unique and incremented by parent..</p>
  
  <p>Can anyone give me some best practices in order to achieve this in an efficient way?</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <pre><code>public class Bar {
      public Guid BarId {get;set;}
      public ICollection&lt;Client&gt; 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 =&gt; new { e.BarId, e.ClientId }).HasName("PK_CLIENT");
                  entity.Property(e =&gt; e.ClientId).ValueGeneratedWhenDefault();
      entity.Property(e =&gt; e.ClientNumber).ValueGeneratedOnAdd();
      entity.HasIndex(e =&gt; new {e.BarId, e.ClientNumber}).IsUnique()
          .HasName("IX_CLIENT_NUMBER");
  }
  </code></pre>
  
  <p>By calling <code>ValueGeneratedOnAdd()</code> 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.</p>