Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Module 11: Entity Framework Core (EF Core)

Module 11 is the bridge between your C# code and the database. Entity Framework Core (EF Core) is a modern Object-Relational Mapper (ORM) that allows you to interact with a database using C# objects, eliminating the need to write most of the SQL code manually.


1. ORM Concepts & EF Core Architecture

An ORM (Object-Relational Mapper) is a layer that sits between your application and the database.

  • The Problem: C# uses Objects (with inheritance and nesting), while SQL uses Tables (with rows and columns). They don’t naturally speak the same language.
  • The Solution: EF Core maps your C# Classes to Database Tables and your C# Properties to Table Columns.

2. The Core Building Blocks: DbContext & DbSet

These are the two most important classes you will work with in EF Core.

  • DbContext: Think of this as the “Gateway” to your database. It manages the connection and tracks changes to your objects.
  • DbSet<T>: Represents a specific table in the database. You use this to perform CRUD (Create, Read, Update, Delete) operations.

C#

public class AppDbContext : DbContext {
    public DbSet<Product> Products { get; set; } // This maps to a 'Products' table
}

3. Code First vs. Database First

There are two primary workflows for managing your data schema:

ApproachDescriptionUse Case
Code FirstYou write C# classes first, and EF Core generates the database for you.New projects; gives developers full control over the model in code.
Database FirstYou have an existing database, and EF Core generates C# classes based on it.Legacy systems or when the database is managed by a separate DBA team.

4. Migrations: Version Control for your Database

Since you are using Code First, what happens when you add a new property to your C# class? You use Migrations.

  1. Add-Migration: EF Core looks at the changes in your C# code and creates a “script” (a C# file) to update the database.
  2. Update-Database: EF Core executes that script against your SQL server.

5. Relationships

In the real world, data is connected. EF Core handles three main types of relationships:

  • One-to-Many: The most common. (e.g., One Category has many Products).
  • One-to-One: (e.g., One User has one UserProfile).
  • Many-to-Many: (e.g., A Student can have many Courses, and a Course has many Students). EF Core handles the “hidden” join table for you automatically.

6. Loading Related Data: Lazy vs. Eager

When you fetch a “Category” from the database, should it also fetch all 500 related “Products” automatically?

  • Eager Loading: You explicitly tell EF Core to grab the related data using the .Include() method.
    • Example: _context.Categories.Include(c => c.Products).ToList();
  • Lazy Loading: Related data is only loaded from the database the moment you try to access it.
    • Warning: This can lead to the N+1 Problem, where your app makes hundreds of tiny database calls, slowing everything down.

7. Performance Optimization

To build professional-grade apps, you must optimize your queries:

  • AsNoTracking(): Use this for “read-only” queries. It tells EF Core not to waste memory tracking changes to the objects.
  • IQueryable vs. IEnumerable: Always filter your data using Where() while it’s still an IQueryable. This ensures the filtering happens inside the Database (SQL), not in your Application Memory (RAM).
  • Select (Projection): Don’t pull the whole row (SELECT *). Only select the columns you actually need.

Outcome: Build Production-Ready Databases

By the end of this module, you will be able to:

  1. Design a complex database schema using only C# classes.
  2. Use Migrations to evolve your database safely over time.
  3. Write high-performance LINQ queries to fetch and save data.

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥