A simple class with an auto-incremented Key
column
public class SomeClass
{
[Key]
public long SomeClassId { get; set;}
}
Normally the SomeClassId
will start and auto-increment from 1. Is there anyway to force the ID to start from a specific number, say 10001?
It was suggested here to execute the CHECKIDENT
command through Sql()
. But I wonder if there are other ways to go about this?
Thanks!
Yes, You can use the Fluent API .sample code
class MyContext : DbContext
{
public DbSet<SomeClass> sample{ get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasSequence<int>("sampleNumber", schema: "shared")
.StartsAt(10001)
.IncrementsBy(1);
modelBuilder.Entity<SomeClass>()
.Property(o => o.SomeClassId)
.HasDefaultValueSql("NEXT VALUE FOR shared.sampleNumber");
}
}