John Doe
John Doe

Reputation: 493

Default value for DatabaseGenerated

I can define a identity primary key with:

public class MyEntity
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set;}
}

But if I don't add data annotations what's the type generated by default? Like this:

public class MyEntity
{
    public int Id { get; set;}
}

Identity or depending on the database when I create the database?

I don't find the subject in the official documentation: https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties

Upvotes: 2

Views: 1079

Answers (1)

Orhun
Orhun

Reputation: 1252

By convention, EF looks for property named YourEntityName + Id or just Id, and knows it's for to specify the PK. So implicitly [Key] and [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attributes performed on this property.

Upvotes: 2

Related Questions