Advertisements
Home > Entity Framework > Data Annotations – ForeignKey Attribute in EF 6 & EF Core

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 one-to-many relationship among entities.

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 Student and Standard entities. To represent this relationship, the Student class includes a property StandardId with reference property Standard and 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 StandardIdin Student entity will automatically become a foreign key property and 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.

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 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.

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 specified the name of the foreign key property StandardRefId. 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.

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.

Hope this help !

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: