Entity Framework Relationships - Microsoft Entity Framework

What is Entity Framework Relationship?

In relational databases, relationship is a state that exists between relational database tables through foreign keys. A Foreign Key (FK) is a column or grouping of columns that is used to create and apply a link between the data in two tables. The following chart covers three tables.

  • Student
  • Course
  • Enrollment


In the above illustration, you can see some sort of association/relationship between tables. There are three types of relationships between tables and the relationship between different tables depends on how the associated columns are defined.

  • One-to-Many Relationship
  • Many-to-Many Relationship
  • One-to-One Relationship

One-to-Many Relationship

  • A one-to-many relationship is the most common type of relationship.
  • In this type of relationship, a row in table A can have many matching rows in table B, but a row in table B can have only one matching row in table A.
  • The foreign key is defined in the table that represents the many end of the relationship.
  • For instance, in the above illustration Student and Enrollment tables have one-to much relationship, each student may have many enrollments, but each enrollment belongs to only one student.

In entity framework, this relationship can be created with code as well. Following is an example of Student and Enrollment classes which are associated with one to much relationship.

In the above code, you can see that Student class contains the collection of Enrollment, but Enrollment class has a single Student Object.

Many-to-Many Relationship

In many-to-many relationship, a row in table A can have many corresponding rows in table B, and vice versa.

  • You can create such a relationship by describing a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.
  • For instance, the Student and Course tables have many-to-many relationship that is well-defined by one-to-many relationship from each of these tables to the Enrollment table.

The following code holds the Course class and the above two classes, i.e., Student and Enrollment.

You can see that both Course class and Student class have groups of Enrollment substances which makes many-to-many relationship via junction class Enrollment.

One-to-One Relationship

  • In a one-to-one relationship, a row in table A can have no more than one corresponding row in table B, and vice versa.
  • A one-to-one relationship is formed if both of the related columns are primary keys or have unique limits.
  • In a one-to-one relationship, the primary key acts also as a foreign key and there is no separate foreign key column for either table.

This type of relationship is not common since most information connected in this way would be all in one table. You might use a one-to-one relationship to −

  • Divide a table with many columns.
  • Isolate part of a table for security reasons.
  • Store data that is short-lived and could be easily deleted by simply deleting the table.
  • Store information that applies only to a subset of the main table.

The following code is to add another class name Student Profile which covers the student email id and password.

You can see that Student object class holds Student Profile navigation material goods and Student Profile contains Student navigation things.

All students have onlyone Email and password to login in university domain. This information can be added to Student table but for security reasons it is divided to another table.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Entity Framework Topics