EntityFrameworkCore Key column auto increment from specific value

c# ef-core-2.2 entity-framework-core

Question

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!

1
3
5/7/2019 12:47:46 PM

Accepted Answer

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");
    }
    }
4
5/7/2019 1:28:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow