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

c# csv entity-framework-core

Question

If you add DataAnnotations "Key" a new key will be created when you execute "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; }
}
1
2
12/4/2017 7:41:32 AM

Accepted Answer

Use [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] annotation on Id field

public class Item 
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Required]
    public Guid Id { get; set; }
    public string Name { get; set; }
}
3
2/13/2018 1:17:20 PM


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