简体   繁体   English

实体框架核心-如何通过编程关闭自动增量?

[英]Entity Framework Core - how to programmability turn off auto increment?

If you add DataAnnotations "Key" a new key will be created when you execute "SaveChanges". 如果添加DataAnnotations“ Key”,则在执行“ SaveChanges”时将创建一个新密钥。 But if you wanted to bootstrap records into your database, how do you turn off the auto-generate a key feature? 但是,如果您想将记录引导到数据库中,如何关闭自动生成关键功能?

public class Item 
{
    [Key]
    [Required]
    public Guid Id { get; set; }
    public string Name { get; set; }
}

Use [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] annotation on Id field 在ID字段上使用[Key,DatabaseGenerated(DatabaseGeneratedOption.None)]批注

public class Item 
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Required]
    public Guid Id { get; set; }
    public string Name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM