The Key Attribute or Primary key attribute maps the property as the primary key column in the database. This attribute is applicable to both Entity Framework & Entity Framework Core. In Entity Framework, we can use it to create Composite Primary key, while in EF Core it is not supported.
Table of Contents
Default Convention
The Entity Framework Core Convention look for the property with the name id
or with the name <className>ID
. It then maps that property to the Primary Key. In case it finds both id
property and <className>ID
property, then id
property is used. The following model creates the table with CustomerID As the primary key.
1 2 3 4 5 6 7 | public class Customer { public int CustomerID { get; set; } public string CustomerName { get; set; } } |
Key Attribute
You can override this behavior using the Data Annotation Key attribute. The Key attribute marks the property as the Primary Key. This will override the default Primary Key. The following code creates the table with CustomerNo
as the Primary key instead if CustomerID
Do not forget to import the following namespace
1 2 3 4 | using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; |
1 2 3 4 5 6 7 8 9 10 | public class Customer { public int CustomerID { get; set; } [Key] public int CustomerNo { get; set; } public string CustomerName { get; set; } } |
Note that the CustomerNo
column created with the Identity
enabled. Key attribute, if applied on the integer column will create the column with the Identity
enabled (autoincrement)
The unsigned data types (uint
) is allowed in EF Core. It is mapped to bigint. Also, EF Core does not create a Identity
column for unsigned data types
Short
data type is mapped to SmallInt
with Identity
column
Key Attribute on a string property
1 2 3 4 5 6 7 8 9 | public class Customer { public int CustomerID{ get; set; } [Key] public string CustomeNo { get; set; } public string CustomerName { get; set; } } |
Enabling / Disabling identity column
You can disable/enable the identity
on the numeric column by using the DatabaseGenerated attribute.
The following code disables the identity
column
1 2 3 4 5 6 7 8 9 10 11 | public class Customer { public int CustomerID { get; set; } [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public int CustomerNo { get; set; } public string CustomerName { get; set; } } |
The following code enables the identity
column
1 2 3 4 5 6 7 8 9 10 11 | public class Customer { public int CustomerID { get; set; } [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CustomerNo { get; set; } public string CustomerName { get; set; } } |
Composite Primary Key
The EF Core does not allow creating the Composite Primary key using Key Attribute.
For Example, the following model will result in an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Customer { [Key] [Column(Order = 1)] public int CustomerID { get; set; } [Key] [Column(Order = 2)] public int CustomerNo { get; set; } public string CustomerName { get; set; } } |
EF Core 3.1
The entity type Customer
has multiple properties with the [Key] attribute. Composite primary keys can only be set using HasKey
in OnModelCreating
.
EF Core 2.1
Entity type Customer
has the composite primary key defined with data annotations. To set the composite primary key, use fluent API.