Curriculum
Relationships in Entity Framework Core are one of the most important concepts in database design and application development. Real-world applications rarely contain isolated tables. Instead, entities are connected to one another through relationships. Understanding Relationships in Entity Framework Core allows developers to build structured, scalable, and efficient database systems.
Whether you are developing a Student Management System, Hospital Management System, Banking Application, E-Commerce Platform, CRM, ERP, Inventory System, or SaaS Product, relationships help organize and connect data logically.
Relationships define how entities are connected to each other.
Example:
Student
↓
Course
A student may belong to a course.
Relationships allow EF Core to understand how data is linked.
Without relationships:
Duplicate Data
Poor Design
Difficult Queries
Data Inconsistency
With relationships:
Structured Data
Reduced Duplication
Efficient Queries
Better Integrity
Relationships improve database quality.
School Management System:
Student
↓
Course
↓
Teacher
Entities interact through relationships.
Entity Framework Core supports:
One-To-One
One-To-Many
Many-To-Many
These three relationship types cover most business requirements.
Definition:
One Record
↓
One Related Record
Example:
Person
↓
Passport
One person has one passport.
One passport belongs to one person.
Person Entity:
public class Person
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public Passport Passport
{
get;
set;
}
}
Passport Entity:
public class Passport
{
public int Id
{
get;
set;
}
public string Number
{
get;
set;
}
public int PersonId
{
get;
set;
}
public Person Person
{
get;
set;
}
}
This creates a One-To-One relationship.
Person
1
↓
1
Passport
Both entities are directly connected.
Definition:
One Record
↓
Many Records
This is the most common relationship type.
Example:
Teacher
↓
Students
One teacher teaches many students.
Each student belongs to one teacher.
public class Teacher
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public ICollection<Student>
Students
{
get;
set;
}
}
The collection represents multiple students.
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public int TeacherId
{
get;
set;
}
public Teacher Teacher
{
get;
set;
}
}
TeacherId acts as the foreign key.
Teacher
1
↓
Many
Students
This structure is common in business applications.
A Foreign Key is a column that references another table.
Example:
public int TeacherId
{
get;
set;
}
Purpose:
Connect Tables
Foreign keys establish relationships.
Navigation Properties allow movement between related entities.
Example:
public Teacher Teacher
{
get;
set;
}
This provides access to related data.
Definition:
Many Records
↓
Many Records
Example:
Students
↓
Courses
A student can enroll in many courses.
A course can contain many students.
Student Entity:
public class Student
{
public int Id
{
get;
set;
}
public ICollection<Course>
Courses
{
get;
set;
}
}
Course Entity:
public class Course
{
public int Id
{
get;
set;
}
public ICollection<Student>
Students
{
get;
set;
}
}
EF Core automatically creates the relationship.
Students
Many
↔
Many
Courses
This relationship is common in education systems.
Navigation properties help access related data.
Examples:
Teacher → Students
Student → Teacher
Student → Courses
Course → Students
They simplify data retrieval.
Eager Loading retrieves related data immediately.
Example:
var students =
context.Students
.Include(
s => s.Teacher)
.ToList();
Result:
Students + Teachers
Data is loaded in a single query.
Benefits:
Fewer Queries
Better Performance
Simpler Code
Eager Loading is widely used.
Lazy Loading loads related data only when needed.
Workflow:
Load Student
↓
Teacher Not Loaded
↓
Access Teacher
↓
Teacher Loaded
Data is fetched on demand.
Explicit Loading loads related data manually.
Example:
Load Student
↓
Load Teacher
↓
Separate Operation
Provides full control over data retrieval.
Relationships can be configured using:
Conventions
Data Annotations
Fluent API
EF Core supports multiple approaches.
Example:
TeacherId
EF Core automatically identifies the foreign key.
This is the simplest approach.
[ForeignKey(
"Teacher")]
public int TeacherId
{
get;
set;
}
Annotations provide explicit configuration.
Example:
modelBuilder
.Entity<Student>()
.HasOne(
s => s.Teacher)
.WithMany(
t => t.Students);
Fluent API provides advanced control.
Student Management System:
Teacher
↓
Students
↓
Courses
↓
Results
Relationships connect all entities.
Doctor
↓
Patients
↓
Appointments
↓
Prescriptions
Relationships organize healthcare data.
Customer
↓
Orders
↓
Products
↓
Payments
Relationships connect business operations.
Customer
↓
Accounts
↓
Transactions
Relationships support financial operations.
Reduces redundancy.
Maintains consistency.
Related data becomes accessible.
Supports complex applications.
Organized data structures.
These benefits are critical in enterprise systems.
Can break relationships.
Causes mapping issues.
Reduces performance.
Creates maintenance problems.
May produce incorrect schemas.
Relationships define how entities connect to each other.
One-To-One, One-To-Many, and Many-To-Many.
A Foreign Key connects one table to another.
A property used to access related entities.
Eager Loading retrieves related data immediately.
They organize data and improve database integrity.
Relationships define how database entities are connected.
One record is connected to exactly one related record.
One record is connected to multiple related records.
Multiple records on both sides are connected to each other.
A Foreign Key is a column that references another table.
They improve database structure, integrity, and data access efficiency.
WhatsApp us