Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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". 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; }
}
like image 509
001 Avatar asked Mar 07 '23 10:03

001


1 Answers

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; }
}
like image 138
Tamás Lévai Avatar answered Mar 25 '23 16:03

Tamás Lévai