Chapter 1: Introduction to Entity Framework Core - IndianTechnoEra
Latest update Android YouTube

Chapter 1: Introduction to Entity Framework Core

Welcome to your journey of mastering Entity Framework Core! This chapter lays the foundation by explaining what EF Core is, why it's essential for a .NET Full Stack Developer, and how it fits into the modern development ecosystem.

1.1 What is an ORM? (Object-Relational Mapper)

To understand EF Core, you must first understand the problem it solves. In software development, there is an impedance mismatch between object-oriented programming (C#) and relational databases (SQL).

The Two Worlds:

C# (Object-Oriented World) SQL (Relational World)
Works with Classes and Objects Works with Tables and Rows
Uses Properties (Id, Name) Uses Columns (Id, Name)
Navigates via object references (blog.Posts) Navigates via Foreign Keys (Post.BlogId)
Inheritance is common Inheritance is not natively supported

ORM Definition: An Object-Relational Mapper is a tool that automatically maps data between relational databases and object-oriented models. It generates SQL behind the scenes and materializes the results back into C# objects.

Analogy: The Translator

Imagine you are an English speaker (C#) and you need to communicate with a person who only speaks Spanish (SQL). An ORM is your bilingual translator. You speak English to the translator, and they speak Spanish to the other person. You don't need to learn Spanish; you just need to know how to work with your translator.


1.2 Why Entity Framework Core? (The Full Stack Perspective)

As a Full Stack Developer, you wear many hats. EF Core makes your life easier in several critical ways:

Key Benefits:

  • 🚀 Productivity: You write C# code instead of SQL strings. This speeds up development significantly. Instead of switching contexts between C# and SQL, you stay in C#.
  • ✅ Type Safety: If you misspell a column name, the C# compiler throws an error immediately, rather than waiting for a runtime database error. This catches bugs at compile time, not in production.
  • 🔄 Change Tracking: EF Core automatically tracks changes to your objects. When you call SaveChanges(), it generates the appropriate INSERT, UPDATE, or DELETE statements automatically. You don't have to write any SQL.
  • 📦 Database Version Control: With Migrations, you can version control your database schema just like you version control your C# code. This enables team collaboration and smooth deployments.
  • 🌐 Cross-Platform: EF Core works on Windows, Linux, and macOS. Your applications can run anywhere.
  • 🎯 LINQ Integration: You use LINQ (Language Integrated Query) which you already know from working with collections. The same syntax works for databases.

1.3 EF Core vs. EF6 (Legacy) vs. Dapper

It's important to know where EF Core stands in the .NET data access ecosystem. Each tool has its place.

Comparison Table:

Feature EF Core EF6 (Legacy) Dapper
Platform Cross-platform (.NET Core / .NET 5+) Windows only (.NET Framework) Cross-platform
Performance Good (optimized over versions) Moderate ⚡ Excellent (closest to raw ADO.NET)
Abstraction Level High (LINQ, Change Tracking) High Low (you write SQL)
Learning Curve Moderate Steep Shallow (if you know SQL)
Change Tracking Yes (Automatic) Yes No (Manual)
LINQ Support Full LINQ Full LINQ No (You write SQL strings)
Migrations Yes (Built-in) Yes No
Best Use Case Line-of-business apps, CRUD-heavy APIs, new projects Maintaining legacy Windows apps High-performance queries, microservices, reporting

When to Choose What?

  • Choose EF Core for most new applications, especially if you have complex CRUD operations and want rapid development. It's the standard choice for enterprise line-of-business applications.
  • Choose Dapper when you need maximum performance for specific queries (e.g., read-heavy dashboards, gaming leaderboards, high-traffic APIs). Many projects use both: EF Core for complex writes and Dapper for high-speed reads.
  • Avoid EF6 for new projects. It's legacy technology and only runs on Windows.

1.4 EF Core Versions and .NET Compatibility

EF Core evolves with .NET. Understanding version compatibility is crucial for project planning.

Version History:

EF Core Version .NET Version Release Date Key Features Introduced
EF Core 2.x .NET Core 2.x 2017-2018 Lazy loading, owned types, global query filters
EF Core 3.x .NET Core 3.x 2019 Many-to-many without join entity (early support), better query translation, Cosmos DB support
EF Core 5.0 .NET 5 2020 Many-to-many, table-per-type (TPT), filtered includes, scrollable windows
EF Core 6.0 .NET 6 (LTS) 2021 Performance improvements (3x faster), temporal tables, pre-configured conventions, SQL Server sparse columns
EF Core 7.0 .NET 7 2022 JSON columns, ExecuteUpdate/ExecuteDelete (bulk updates), table-valued functions
EF Core 8.0 .NET 8 (LTS) 2023 Complex types, raw SQL queries for unmapped types, lazy loading for no-tracking queries, date/time conversions
EF Core 9.0 .NET 9 2024 Enhanced JSON support, better AOT compilation compatibility

For this tutorial, we will use EF Core 8.0 with .NET 8.0 (the latest LTS version as of 2025). LTS versions provide 3 years of support, making them ideal for enterprise applications.


1.5 The Two Main Workflows

EF Core supports two approaches to defining the model-database relationship. Understanding both helps you choose the right approach for your project.

1. Database-First (Reverse Engineering)

In this workflow, you start with an existing database. You run a command, and EF Core generates the entity classes and DbContext for you.


[Existing Database] ---Scaffold---> [C# Entity Classes]

When to use:

  • Working with a legacy database that already exists.
  • When a DBA team strictly controls the database schema and you cannot change it.
  • Migrating an old application (like ASP.NET Web Forms) to .NET Core.
  • When the database is shared across multiple applications.

Example Command:


dotnet ef dbcontext scaffold "Server=.;Database=MyDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

This command reads the database schema and generates C# classes in the Models folder.

2. Code-First (Our Focus)

This is the modern approach. You write your C# entity classes first. Then you use EF Core Migrations to generate and evolve the database schema.


[C# Entity Classes] ---Migration---> [Database Schema]

When to use:

  • Starting a brand new project from scratch.
  • When you want full control over your domain model without database constraints.
  • When following Domain-Driven Design (DDD) principles.
  • In agile projects where requirements evolve rapidly.

Benefits:

  • You work entirely in C# - no SQL required.
  • Database schema is derived from your clean domain model.
  • Easier to refactor and evolve as requirements change.
  • Full version control of database changes through migrations.
  • Better testability with in-memory databases.

Throughout this tutorial, we will exclusively use the Code-First approach as it's the industry standard for new development.


1.6 How EF Core Works (High-Level Architecture)

Understanding the internal flow helps you debug issues later and write better queries.


┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐

│                 │     │                 │     │                 │

│   Your Code     │────▶│   DbContext     │────▶│   Change Tracker│

│  (LINQ Queries) │     │                 │     │                 │

│                 │     │                 │     │                 │

└─────────────────┘     └─────────────────┘     └────────┬────────┘

                                                          │

                                                          ▼

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐

│                 │     │                 │     │                 │

│   Database      │◀────│   SQL Generator │◀────│   Expression    │

│   (SQL Server)  │     │  (Provider)      │     │   Tree Parser   │

│                 │     │                 │     │                 │

└─────────────────┘     └─────────────────┘     └─────────────────┘

Step-by-Step Flow:

  1. You write LINQ queries against DbSet<T> properties. For example:

    
    var books = context.Books.Where(b => b.Price > 20).ToList();
  2. EF Core builds an Expression Tree representing your query. This is a data structure that represents the logic of your LINQ query.

  3. The Expression Tree is parsed by the query pipeline and translated to a database-specific SQL.

  4. The SQL is generated by the database provider (e.g., Microsoft.EntityFrameworkCore.SqlServer) and executed against the database.

  5. Results are materialized back into C# objects. The database rows are converted to instances of your entity classes.

  6. The Change Tracker monitors your objects for modifications. When you call SaveChanges(), it generates appropriate UPDATE, INSERT, or DELETE statements.

Key Components:

  • DbContext: The primary class that coordinates EF Core functionality. Represents a session with the database.
  • DbSet<T>: Represents a collection of entities of a specific type (typically corresponding to a table).
  • ChangeTracker: Keeps track of changes made to entities after they've been queried from the database.
  • Database Provider: Translates LINQ to SQL for a specific database (SQL Server, PostgreSQL, SQLite, etc.).

1.7 Setting the Stage: Our Learning Project

Throughout this tutorial, we will build a Book Store Management System. This project will evolve as we learn new concepts, from simple to complex.

Initial Domain Concepts:

  • Book: The main entity representing a book in the store.
    • Id (unique identifier)
    • Title (book title)
    • Author (author name)
    • Price (selling price)
    • PublishedOn (publication date)
  • Author: Will be introduced later when we learn relationships.
    • One author can write many books (One-to-Many)
  • Category: For many-to-many relationships.
    • A book can belong to multiple categories (e.g., Fiction, Mystery, Science)
    • A category can contain many books
  • Review: Customer reviews for books.
    • Each review belongs to one book
    • Contains rating and comment

This real-world context will make learning more practical and memorable. As we progress through each chapter, we'll add more complexity to this domain model.


1.8 Prerequisites for This Tutorial

Before proceeding to Chapter 2, ensure you have the following installed:

Required Software:

  • .NET 8.0 SDK: Download from https://dotnet.microsoft.com/download
  • Visual Studio 2022 (or later): Community edition is free. Ensure you select the "ASP.NET and web development" workload.
    Alternative: Visual Studio Code with C# extensions.
  • SQL Server: Any edition works:
    • SQL Server Express LocalDB (installs with Visual Studio)
    • SQL Server Developer Edition (free)
    • SQL Server in Docker container
  • SQL Server Management Studio (SSMS) or Azure Data Studio: To view your database.

Verification Commands:

Open a terminal/command prompt and run:


dotnet --version

This should show 8.0.x or higher.


sqlcmd -S (localdb)\MSSQLLocalDB -Q "SELECT @@VERSION"

This verifies SQL Server LocalDB is accessible.


1.9 Common Terminology You'll Encounter

Term Definition
Entity A C# class that maps to a database table. Each instance maps to a row.
DbContext The main class that coordinates database operations.
DbSet<T> A property in DbContext that represents a table. Used to query and save entities.
Migration A set of instructions to update the database schema to match your model.
Fluent API A way to configure your model using code (inside OnModelCreating).
Data Annotations Attributes you place on your entity classes to configure mapping.
Change Tracker The component that tracks changes to entities after they're loaded.
Navigation Property A property that represents a relationship to another entity (e.g., Book.Author).
Foreign Key A property that holds the ID of a related entity (e.g., Book.AuthorId).

Chapter Summary

Concept Key Takeaway
ORM A translator between C# objects and database tables. Saves you from writing SQL manually.
EF Core Microsoft's modern, cross-platform ORM for .NET. The recommended choice for new applications.
EF Core vs Dapper EF Core for productivity and complex operations, Dapper for raw performance when needed.
Code-First Write C# classes first, generate database from them. Modern approach for new projects.
Database-First Generate C# classes from an existing database. Useful for legacy systems.
Version Compatibility EF Core 8.0 works with .NET 8.0 (LTS). Always match versions appropriately.

Key Points to Remember:

  • EF Core is not the only option, but it's the most productive for most business applications.
  • The Code-First approach gives you maximum control and is the industry standard.
  • Understanding how EF Core translates LINQ to SQL helps you write efficient queries.
  • We'll be building a Book Store application throughout this tutorial to apply concepts in real scenarios.

What's Next?

In Chapter 2: Your First EF Core Application, we will get our hands dirty. We will:

  • Create a new .NET console project
  • Install the required NuGet packages (Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Tools)
  • Define our first entity class (Book)
  • Create a custom DbContext class
  • Configure the connection string
  • Add our first book to the database
  • Run the application and verify data in SQL Server

This will be your first working EF Core application. You'll see exactly how all the pieces we discussed in this chapter come together in working code.


Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.