0
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AccountNumber { get; set; }

This code generates the primary key with values from 1 and so on and increments by 1. Is there anyway that I can provide start value for it?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425

1 Answers1

0

For that it's best to use a sequence instead of an IDENTITY column, eg:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasSequence<int>("AccountNumbers", schema: "dbo")
        .StartsAt(1000)
        .IncrementsBy(1);

    modelBuilder.Entity<Account>()
        .Property(o => o.AccountNumber)
        .HasDefaultValueSql("NEXT VALUE FOR dbo.AccountNumbers");
}

EF Core - Sequences.

For IDENTITY you could call DBCC CHECKIDENT in a Custom Migration.

David Browne - Microsoft
  • 66,275
  • 5
  • 31
  • 57