The ForeignKey Attribute in Entity Framework Core - Code First Approach

Views: 801
Comments: 0
Like/Unlike: 0
Posted On: 05-Jan-2020 00:41 

Share:   fb twitter linkedin
Rahul M...
Moderator
34 Points
17 Posts

Introduction

The ForeignKey attribute is applied to configure a foreign key in the relationship between two entities in EF 6 and EF Core.
It overrides the default behaviour. As per the default behaviour, EF treats 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).


Default behaviour i.e. without ForeignKey attribute

public class Author
{
  public int AuthorId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public ICollection<Book> Books { get; set; }
}
public class Book
{
  public int BookId { get; set; }
  public string Title { get; set; }
  public Author Author { get; set; }
  public int AuthorId { get; set; }
}

A property name AuthorId in Book entity matches with the primary key property of Author entity, so AuthorId in Book entity will automatically become a foreign key property and the corresponding column in the db table will also be a foreign key column.

Why do we need ForeignKey attribute?

The [ForeignKey] attribute overrides the default behaviour for a foreign key.
So we can use it to specify the foreign key property in the dependent entity whose name does not match with the primary key property of the master entity.

How to apply ForeignKey attribute?

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

  1. In the dependent entity class by passing the name of the navigation property:
    public class Author
    {
      public int AuthorId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public ICollection<Book> Books { get; set; }
    }
    public class Book
    {
      public int BookId { get; set; }
      public string Title { get; set; }
      public Author Author { get; set; }
      [ForeignKey("Author")]
      public int AuthorFKId { get; set; }
    }​
  2. In the dependent entity class by passing the name of the foreign key property:
    public class Author
    {
      public int AuthorId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public ICollection<Book> Books { get; set; }
    }
    public class Book
    {
      public int BookId { get; set; }
      public string Title { get; set; }
      [ForeignKey("AuthorFKId")]
      public Author Author { get; set; }
      public int AuthorFKId { get; set; }
    }​
  3. In the principal/master entity class by passing the name of the foreign key property of the dependent entity class:
    public class Author
    {
      public int AuthorId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      [ForeignKey("AuthorFKId")]
      public ICollection<Book> Books { get; set; }
    }​
    public class Book
    {
      public int BookId { get; set; }
      public string Title { get; set; }
      public Author Author { get; set; }
      public int AuthorFKId { get; set; }
    }

Note: We can place multiple foreign keys separating by each one with a comma.

Conclusion

In the article, we understood the use of ForeignKey attribute with help of different examples. Hope it will help to apply it in code-first-approach.

0 Comments
  
 Log In to Chat