Search code examples
c#.netef-code-firstentity-framework-4.1

EF Code First - how to set identity seed?


I have a entity class

public class Employee
{
    public long Id { get; set; }
    public string Name { get; set; }
}

I have set the Id field as the primary key with auto number generation

modelBuilder.Entity<Employee>().HasKey(e => e.Id);
modelBuilder.Entity<Employee>().Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

But I want the Identity to seed from 10000 instead of from 1 which is the default. How can I specify this in EF?


Solution

  • If you are using SQL Server you must create custom database initializer and manually execute DBCC CHECKIDENT ('TableName', RESEED, NewSeedValue). For creating and using custom initializer with custom SQL commands check this answer.