Search code examples
c#.netsql-serverentity-framework-6code-first

How to set starting value for an Identity primary key in Entity Framework code first?


In my project I am creating the Appointments table using Entity Framework code first. When I set my integer primary key, AppointmentNo as [Key] and [Required], it creates the table with initial primary key starting with 1.

I want the appointment numbers to start at 1000. My database is SQL Server. Please help me. Thank you in advance.

[DataContract]
public class Appointment
{
    [DataMember]
    [Key]
    [Required]
    public int AppointmentNo { get; set; }

    [DataMember]
    [Required]
    public string DoctorUN { get; set; }

    [DataMember]
    [Required]
    public string PatientUN { get; set; }
}

Solution

  • It's not possible using ef annotations,but you can execute sql in migration UP method

    Sql("DBCC CHECKIDENT ('Appointment', RESEED, 1000)");