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

.net c# code-first entity-framework-6 sql-server

Question

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; }
}
1
0
9/17/2018 7:11:22 PM

Accepted Answer

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

Sql("DBCC CHECKIDENT ('Appointment', RESEED, 1000)");
2
9/17/2018 7:28:54 PM

Popular Answer

As long as the type of the primary key property is numeric or GUID, Code First will, by convention, automatically configure the key as an identity column.

from:EF and identity columns



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