如何首先在Entity Framework代码中设置Identity主键的起始值?

问题描述 投票:0回答:2

在我的项目中,我首先使用Entity Framework代码创建Appointments表。当我将整数主键AppointmentNo设置为[Key][Required]时,它会创建具有以1开头的初始主键的表。

我希望约会号码从1000开始。我的数据库是SQL Server。请帮我。先感谢您。

[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; }
}
c# .net sql-server entity-framework-6 code-first
2个回答
2
投票

使用ef注释是不可能的,但您可以在迁移UP方法中执行sql

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

0
投票

只要主键属性的类型是数字或GUID,Code First将按照惯例自动将键配置为标识列。

来自:EF and identity columns

© www.soinside.com 2019 - 2024. All rights reserved.