Data Annotations - ForeignKey Attribute in EF 6 & EF Core

The ForeignKey attribute is used to configure a foreign key in the relationship between two entities in EF 6 and EF Core. It overrides the default conventions. As per the default convention, EF makes a property as foreign key property when its name matches with the primary key property of a related entity.

ForeignKey Signature: [ForeignKey(name string)]

  • name: Name of the associated navigation property or the name of the associated foreign key(s).

Consider the following example of a one-to-many relationship among entities.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

The above example depicts a one-to-many relationship between the Student and Standard entities. To represent this relationship, the Student class includes a property StandardId with reference property Standard, and the Standard entity class includes collection navigation property Students. A property name StandardId in Student entity matches with the primary key property of Standard entity, so StandardId in Student entity will automatically become a foreign key property and the corresponding column in the db table will also be a foreign key column, as shown below.

Entity Framework code-first example

The [ForeignKey] attribute overrides the default convention for a foreign key It allows us to specify the foreign key property in the dependent entity whose name does not match with the primary key property of the principal entity.

The [ForeignKey(name)] attribute can be applied in three ways:

  1. [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity
  2. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity
  3. [ForeignKey(ForeignKeyPropertyName)] on the navigation property in the principal entity

[ForeignKey] on the foreign key property in the dependent entity

The [ForeignKey] on the foreign key property in the dependent entity and the related navigation property name can be specified as a parameter as shown below.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey("Standard")]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the StandardRefId and specified in the name of the navigation property Standard. This will create the foreign key column named StandardRefId in the Students table, preventing the generation of a StandardId column in the database.

Entity Framework code-first example

[ForeignKey] on the navigation property in the dependent entity

The [ForeignKey] attribute can be applied to the navigation property and the related foreign key property name can be specified as shown below.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    
    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the Standard navigation property and the name of the foreign key property StandardRefId is specified. This will create the foreign key column named StandardRefId in the Students table, preventing the generation of a StandardId column in the database.

[ForeignKey] on the navigation property in the principal entity

The [ForeignKey] attribute can be applied to the navigation property in the principal entity and the related foreign key property name can be specified in the dependent entity, as shown below.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    [ForeignKey("StandardRefId")]
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the Students navigation property in the principal entity Standard. This will create a foreign key column StandardRefId in the Students table in the database.