Curriculum
Code First, Database First, Migrations, Relationships, and Data Annotations in Entity Framework Core are among the most important concepts for professional .NET developers. Modern ASP.NET Core Applications, Enterprise Software Systems, E-Commerce Platforms, Banking Applications, CRM Systems, ERP Solutions, Hospital Management Systems, and School Management Systems use these concepts extensively to design, manage, and maintain databases efficiently.
Understanding Code First, Database First, Migrations, Relationships, and Data Annotations in Entity Framework Core helps developers build scalable, maintainable, and production-ready applications.
Code First is an approach where developers create C# classes first and Entity Framework Core automatically generates the database structure.
Workflow:
Create C# Classes
↓
Create DbContext
↓
Generate Migration
↓
Create Database
The database is created from code.
This is the most popular approach in modern ASP.NET Core development.
Benefits:
Most new applications use Code First.
Entity:
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
This class becomes a database table.
public class
ApplicationDbContext :
DbContext
{
public DbSet<Student>
Students
{
get;
set;
}
}
EF Core uses DbContext to create the database structure.
Database First is the opposite approach.
Workflow:
Create Database
↓
Create Tables
↓
Generate Models
↓
Generate DbContext
The database already exists before coding begins.
Database First is useful when:
Many enterprise environments use Database First.
Existing Table:
CREATE TABLE Students
(
Id INT,
Name NVARCHAR(100),
Course NVARCHAR(100)
)
Generate Models:
Scaffold-DbContext
EF Core creates C# classes automatically.
| Code First | Database First |
|---|---|
| Start with C# Classes | Start with Database |
| Modern Approach | Legacy Systems |
| Developer Driven | Database Driven |
| Easier Version Control | Existing Databases |
| Common in ASP.NET Core | Common in Enterprise Systems |
Both approaches are widely used.
Migrations track database schema changes over time.
Without Migrations:
Manual Database Changes
With Migrations:
Version Controlled Database Changes
Migrations make database evolution easier.
Benefits:
Migrations are essential for professional development.
Package Manager Console:
Add-Migration
InitialCreate
Output:
Migration Created
EF Core compares models and generates SQL automatically.
Example:
migrationBuilder.CreateTable(
name: "Students",
columns: table => new
{
Id = table.Column<int>(),
Name = table.Column<string>()
});
This file describes database changes.
Command:
Update-Database
Output:
Database Updated
The migration is applied to SQL Server.
Original:
public string Name
{
get;
set;
}
Add:
public string Email
{
get;
set;
}
Generate Migration:
Add-Migration
AddEmailColumn
Update Database:
Update-Database
The Email column is created automatically.
Example:
Remove-Migration
Useful when a migration was created incorrectly.
Relationships define how tables connect.
Common relationship types:
One-to-One
One-to-Many
Many-to-Many
Relationships are fundamental to relational databases.
Example:
Student
StudentProfile
One Student:
One Profile
One Profile:
One Student
Student:
public class Student
{
public int Id
{
get;
set;
}
public StudentProfile
Profile
{
get;
set;
}
}
Profile:
public class StudentProfile
{
public int Id
{
get;
set;
}
}
EF Core creates the relationship automatically.
Most common relationship.
Example:
One Course
Many Students
A Course can contain many students.
Course:
public class Course
{
public int Id
{
get;
set;
}
public ICollection<Student>
Students
{
get;
set;
}
}
Student:
public class Student
{
public int Id
{
get;
set;
}
public int CourseId
{
get;
set;
}
public Course Course
{
get;
set;
}
}
EF Core creates the foreign key automatically.
Example:
Students
Courses
One Student:
Many Courses
One Course:
Many Students
Many-to-Many relationships are common in educational systems.
Student:
public ICollection<Course>
Courses
{
get;
set;
}
Course:
public ICollection<Student>
Students
{
get;
set;
}
EF Core automatically creates a junction table.
Data Annotations configure models using attributes.
Namespace:
using
System.ComponentModel
.DataAnnotations;
These attributes define validation and database rules.
Example:
[Required]
public string Name
{
get;
set;
}
Meaning:
Cannot Be Null
Useful for validation.
Example:
[StringLength(100)]
public string Name
{
get;
set;
}
Maximum:
100 Characters
Database column length is configured automatically.
Example:
[Key]
public int StudentId
{
get;
set;
}
Defines the primary key.
Example:
[Table("Students")]
public class Student
{
}
Custom table name specified.
Example:
[Column("StudentName")]
public string Name
{
get;
set;
}
Custom column name configured.
Example:
[NotMapped]
public string FullName
{
get;
set;
}
EF Core ignores this property.
No database column is created.
Configured inside models.
Example:
[Required]
Configured inside DbContext.
Example:
modelBuilder.Entity<Student>()
.Property(
s => s.Name)
.IsRequired();
Fluent API offers greater flexibility.
Customers
Accounts
Transactions
Products
Orders
Customers
Patients
Doctors
Appointments
Students
Courses
Results
Relationships are essential in these systems.
Database changes will not be applied.
Can create duplicate data.
Complex configurations are better handled with Fluent API.
May cause data integrity issues.
Can conflict with migrations.
Code First creates databases from C# classes.
Database First generates C# models from existing databases.
Migrations track and apply database schema changes.
One parent record relates to many child records.
Attributes used to configure validation and database behavior.
They provide version-controlled database schema management.
Code First creates databases from C# classes and models.
Database First generates models from existing databases.
Migrations track database schema changes and apply updates automatically.
A relationship where one record is associated with multiple related records.
Attributes used to define validation rules and database mappings.
They help developers design, maintain, and scale database-driven applications efficiently.
WhatsApp us