Updated February 14, 2023
Introduction to Entity Framework Relationships
Entity Framework Relationships exist between the relational database tables through foreign keys. Foreign Key is the column or the combination of columns used to begin and implement the relation between the data in two tables. In a relational database, there are three types of relationships between the tables of the database: One-to-One, Many-to-Many, and One-to-Many. The relationship enables the relational databases to divide and store the data in several tables while relating different data items.
Overview of Entity Framework Relationships
The relationship in the context of databases is the circumstances that stay alive between two relational database tables when one table has a foreign key to facilitate references to the primary key of the other table. The relationship enables the relational databases to divide and save the data in several tables while relating different data items.
Let’s consider one example: we are storing the information about the People and their Addresses, then we are required to build two tables; one is to store the People’s information like ID, name, and another one for PeopleAddress contains the address, city, state, and so on. Both People and PeopleAddress have a One-to-Many relationship so that we can retrieve the People information based on the relations.
The relationship in the Entity Framework describes how several entities are associated with other entities in the database. Creating a relationship between two entities is called the Principle Entity (main entity); it is the main entity in the relationship. Dependant Entity (other entity) contains the Foreign Key reference to the primary key of the Principle Entity after the application is initiated.
Entity Framework Relationships Diagram
Entity Framework Relationships exist between the relational database tables throughout the foreign keys.
The relationships between the entities in Entity Framework there are three types of relationships supported by Entity Framework they are as follows:
- One-to-One
- One-to-Many
- Many-to-Many
Let’s see the created Entity Data Model for the InformationDB database in the following diagram; it contains the entity data model with all the relationships and entities.
In the relational database, the relationship exists between the relational databases through Foreign Keys. Foreign Key (FK) is the combination of columns to create connections between the data in two tables. For example, the above diagram contains several tables like Student, StudentAddress, Course, Standard, and Teacher. In this diagram, there are some sorts of associations between each table. Like Tea For example, like and Course tables have One-to-Many relationships; one teacher can handle many courses for the students; likewise, different relationships may occur.
Consider Student and StudentAddress table; it contains the StudentID; there is a One-to-One relationship in both tables because one particular student has only one address; likewise, there are relations with each table.
Types of Entity Framework Relationships
In Entity Framework Relationships, there are three types of relationships they are as follows:
- One-to-One Relationship
- One-to-Many Relationship
- Many-to-Many Relationship
1. One-to-One Relationship
It defines that the row in table1 has not more than one matching row in table2 and the same as vice versa. The relationship is generated only if both related columns are primary keys or if it has unique constraints. In this relationship, the Primary Key (PK) acts as Foreign Key (FK), and there is no other individual FK column for both tables.
This One-to-One relationship is like this:
- Dividing table with several columns.
- For security purposes, isolate elements of the table.
- To store detail, this applies to only a subset of the main table.
For example, consider the below code, which is a new class UserDetail it contains only student email-id and password.
Code:
public class StudentMaster
{
public int StuID { get; set; }
public string StuLastName { get; set; }
public string StuFirstMidName { get; set; }
public DateTime StuEnrollmentDate { get; set; }
public virtual ICollection<EnrollmentMaster> Enrollments { get; set; }
public virtual UserDetail userDetail { get; set; }
}
public class UserDetail
{
public UserDetail() { }
public int userID { get; set; }
public string userEmail { get; set; }
public string userPassword { get; set; }
public virtual StudentMaster Student { get; set; }
}
The above StudentMaster Entity Class holds the userDetails navigation property, and the UserDetail owns the Student navigation property. In addition, every student has their email/ login ID and password to log in to the Campus Domain. So there, details can be included in the StudentMaster table, but for security purposes, it is divided into another table.
2. One-to-Many Relationship
It is the general type of relationship; in this relationship, a row in table1 can have several matching rows in table2, but the row in table2 can have only one matching in table1. The table defined the Foreign Key (FK) that denotes many relationships. Consider one example code: StudentMaster and EnrollmentMaster Classes; they are associated with One-to-Many Relationships.
Code:
public class StudentMaster
{
public int StuID { get; set; }
public string StuLastName { get; set; }
public string StuFirstMidName { get; set; }
public DateTime StuEnrollmentDate { get; set; }
public virtual ICollection<EnrollmentMaster> Enrollments { get; set; }
}
public class EnrollmentMaster
{
public int EnID { get; set; }
public int CuID { get; set; }
public int StuID { get; set; }
public Grade? eGrade { get; set; }
public virtual CourseMaster Course { get; set; }
public virtual StudentMaster Student { get; set; }
}
In this code, the StudentMaster Class holds the Collection of EnrollmentMaster, but that particular class contains only a single Student Object.
3. Many-to-Many Relationship
In this, a row in table1 contains several identical rows in table2; likewise, table2 contains several identical rows in table1; vice versa. To create a relationship by describing the third table, called the junction table, their primary key consists of foreign keys from two tables, 1 and 2.
Consider one example of the StudentMaster and CourseMaster having the relationship of many-to-many described by the relationship one-to-many from each table to the EnrolmentMaster table.
The code below holds the Course Class and StudentMaster and EnrollmentMaster as follows:
Code:
public class CourseMaster
{
public int CuID { get; set; }
public string cTitle { get; set; }
public int cCredits { get; set; }
public virtual ICollection<EnrollmentMaster> Enrollments { get; set; }
}
Both Classes (CourseMaster and StudentMaster) contain collections of Enrollment Objects, making the relationship Many-to-Many through the junction class EnrollmentMaster.
Conclusion
We have seen Entity Framework Relationships in this article, which contains various relationships with examples.
Recommended Articles
This is a guide to Entity Framework Relationships. Here we discuss the introduction, entity framework relationships diagram, and types. You may also have a look at the following articles to learn more –