I've seen many posts and answers regarding how to mark a field as the identity column. Many of them are outdated and are targeting older versions of Entity Framework.
Some resources tell me to use an attribute on the field:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
Other resources tell me to add this code to OnModelCreating
method:
modelBuilder.Entity<User>().Property(u => u.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
Which one should I use? First, second, both, doesn't matter, or something else?
modelBuilder
for more complex scenarios the attributes don't cover. They're both equally valid, and you'll see that under the covers (EF 'conventions'), the attributes are used to tell themodelBuilder
what to do. – PervertKey
attribute (I don't think I need it anyway as the property name isID
) – Choral