EF Code First - how to set identity seed?

Achinth Gurkhi picture Achinth Gurkhi · May 12, 2011 · Viewed 27.6k times · Source

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?

Answer

Ladislav Mrnka picture Ladislav Mrnka · May 12, 2011

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.