The Key Attribute or Primary key attribute maps the property as the primary key column in the database. We can also use it to create a composite Primary key.
Table of Contents
Default Convention
Entity Framework Default conventions 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. Entity Framework Data Annotation Key attribute marks the property as 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
) are not allowed in EF as Primary key. Infact Entity Framework does not support unsigned
data types
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
When Primary Key consists of more than on Property it is called a Composite primary key. We can apply the Key attribute on all the candidate fields to denote them as Composite Key. In such a case we must also specify the Column Order of the appearance of the key.
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; } } |
If you do not specify the Column Order Attribute, then the entity framework will raise the error “Unable to determine
Few important notes on Composite Primary key
- You can use any integer values in order attribute. For example, 10 and 15 are perfectly acceptable instead of 1 and 2.
- If we apply Composite Key on integer or GUID columns, it does not create an Identity column.
- There is no restriction on the data type of the key except for the unsigned data types