Relationships in Entity Framework Core

Relationships Between Entities in Entity Framework Core

In this article, I am going to discuss the Relationships Between Entities in the Entity Framework Core, i.e., at the end of this article, you will understand how the entity framework manages the relationships between entities. Please read our previous article, where we discussed Fluent API Configurations in Entity Framework Core with Examples.

What are Relationships?

A common requirement in relational databases is to establish relationships between database tables or entities. And whenever we try to establish a relationship between two entities, then one of the entities will act as a Principal Entity, and the other Entity will act as the Dependent Entity.

Let us first understand what is Principal Entity and Dependent Entity from the relational database point of view.

  1. Principal Entity: The Entity containing the Primary or Unique Key properties is called Principal Entity.
  2. Dependent Entity: The Entity which contains the Foreign key properties is called Dependent Entity.

From the Relational Database Point of view, we already know that the Relationships Between two entities or tables are established using Foreign Keys. A Foreign Key is a Key that refers to the Unique Key or Primary Key of another table. Once we establish the relationships between database tables or entities, then we can get meaningful results from the database.

According to Database Normalization, we should not store all the information in a single database table. We need to split the data into multiple tables, but there should be some link or relationship between the tables.

Before understanding the different types of relationships, let us first understand a few important terms which are used in database and model classes.

  1. Primary Key: The Primary key is a column (columns in the case of composite primary key) in the database Table that Uniquely identifies each row in the table.
  2. Foreign Key: The Foreign key is a column in a table that makes a relationship with another table. The Foreign key Column should point to the Principal Entity’s Primary Key or Unique Key column.
  3. Navigation Properties: In .NET, the Navigation properties define the type of relationships between the Entities. Based on the requirements, these properties are defined either in the Principal Entity or in Dependent Entity.

The Navigation Properties are again classified into two types. They are as follows:

  1. Reference Navigation Property: This is a property that refers to a Single Related Entity, i.e., it is used to implement a One-to-One relationship between two entities.
  2. Collection Navigation Property: This is a property that refers to a Collection of Entities, i.e., it is used to implement One-to-Many or Many-to-Many relationships between two entities.

Types of Relationships in Database:

In a relational database, a relationship refers to the association or connection between two or more tables based on common data elements. Relationships are fundamental to organizing and structuring data to enable efficient querying and retrieval of information. They define how data in different tables is related to each other, allowing you to perform complex queries and analyze data effectively.

There are several types of relationships in a relational database:

  1. One-to-One (1:1) Relationship: In this type of relationship, each record in one table is associated with exactly one record in another table, and vice versa. This is often used when two entities have a unique relationship.
  2. One-to-Many (1:N) Relationship: In a one-to-many relationship, a single record in one table is related to multiple records in another table. However, each record in the second table is related to only one record in the first table. This is the most common type of relationship.
  3. Many-to-Many (N:N) Relationship: A many-to-many relationship exists when multiple records in one table are related to another. This type of relationship is typically implemented using a junction table or associative entity, which breaks down the relationship into two one-to-many relationships.
  4. Self-Referencing Relationship: This type of relationship occurs when records in a single table are related to other records in the same table. For example, in an organizational structure, an employee might have a manager who is also an employee.

Properly defining and maintaining relationships in a database is crucial for data integrity, consistency, and accuracy. It allows us to avoid data duplication, reduce redundancy, and create a well-structured and normalized database schema. Relational databases, such as MySQL, Oracle, PostgreSQL, and Microsoft SQL Server, provide mechanisms for defining and enforcing these relationships through their table creation and modification features.

Types of Relationships in Entity Framework Core:

In relational databases, there are four types of relationships between the database tables (One-to-One, One-to-Many, Many-to-Many, and Self-Referencing Relationship).

Entity Framework Core (EF Core) is an Object-Relational Mapping (ORM) framework for .NET that enables developers to work with databases using object-oriented concepts. EF Core defines relationships between entities (classes that represent database tables) using navigation properties and annotations. The Entity Framework supports four types of relationships similar to the database. They are as follows:

  1. One-to-One Relationship
  2. One-to-Many Relationship
  3. Many-to-Many Relationship
  4. Self-Referencing Relationship
One-to-One (1:1) Relationship:

EF Core establishes a one-to-one relationship when both entities have a reference navigation property pointing to the other entity. Annotations like [ForeignKey] and [InverseProperty] are often used to define the relationship explicitly.

In the One-To-One relationship, a row in Table A can have no more than one matching row in Table B, and vice versa. A one-to-one relationship is created if both of the related columns are primary keys or have unique constraints. In the one-to-one relationship, the primary key acts additionally as a foreign key, and there is no separate foreign key column.

One-to-Many (1:N) Relationship:

This type of relationship is established when one entity has a collection navigation property pointing to a reference navigation property. The related entity often has a reference navigation property pointing back to the single entity (the “one” side).

The One-To-Many relationship is the most common type of relationship between database tables or entities. In this type of relationship, a row in one table, let’s say TableA, can have many matching rows in another table, let’s say TableB, but a row in TableB can have only one matching row in TableA.

Many-to-Many (N:N) Relationship:

EF Core represents many-to-many relationships by introducing a junction (join) table in the database schema. Both entities have collection navigation properties pointing to related entities.

In many-to-many relationships, a row in one table, let’s say TableA, can have many matching rows in another table, let’s TableB, and vice versa is also true, i.e., a row in TableB can have many matching rows in TableA. In Relational Databases, we can create many-to-many relationships by defining a third table, whose primary key consists of the foreign keys from tableA and tableB. So, here, the Primary Key is a composite Primary key.

Self-Referencing Relationship:

Similar to a self-referencing relationship in a relational database, an entity can have a relationship with instances of the same entity type. For example, an Employee entity can have a Manager navigation property that points to another Employee. The [ForeignKey] attribute or fluent API is used to configure the relationship.

In the next article, I am going to discuss How to Configure One-to-One Relationships in Entity Framework Core using Fluent API with Examples. In this article, I explain the basic concepts of Relationships between Entities in the Entity Framework Core. I hope you enjoyed this Relationship in EF Core Fluent API article.

Leave a Reply

Your email address will not be published. Required fields are marked *