Google Sign-In Required for .NET Guided Labs

To verify code authenticity, store your pushed GitHub repositories, and earn the .NET Enterprise certification, please sign in with your student account.

Day 15Sprint 2 • EF Core & Database
2 Hours

Day 15: Entity Framework Core 9, DbContext & Code-First Migrations

Entity Framework Core is Microsoft's official Object-Relational Mapper (ORM). Instead of writing raw SQL strings, you interact with your database using strongly-typed C# domain models. EF Core translates your C# LINQ queries into optimized SQL queries automatically.

Install Microsoft.EntityFrameworkCore.SqlServer and Design tools
Create DbContext class and configure connection strings
Run dotnet ef migrations add and dotnet ef database update
Perform async CRUD operations (Create, Read, Update, Delete) on SQL Server

Core Architectural Insights

  • DbContext represents a session with the database
  • DbSet<T> represents a database table
  • Code-First Migrations track schema changes in versioned C# migration files
  • Always use async methods (ToListAsync, SaveChangesAsync) in web applications

Real-World Developer Analogy

EF Core acts as a universal translator between the C# object-oriented world (classes, lists) and the SQL relational world (tables, rows, foreign keys).