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
  • 732,580
  • 175
  • 1,330
  • 1,459
Nikhil
  • 13
  • 1
  • 6

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
  • 80,331
  • 6
  • 39
  • 67