Roadmap › 1. .NET › Step 15
EF

Entity Framework (EF6 & EF Core)

DbContext, code-first and database-first, conventions and Fluent API, relationships, migrations, querying and loading strategies, change tracking, concurrency, transactions, performance tuning and EF6 vs EF Core.
What it is & where it is used

Entity Framework Core (EF Core) is Microsoft's open-source ORM (Object-Relational Mapper) for .NET. You work with C# classes (entities) and LINQ; EF Core generates SQL, tracks changes and maps results back to objects.

Supported databases (providers): SQL Server / Azure SQL, SQLite, PostgreSQL (Npgsql), MySQL/MariaDB (Pomelo), Oracle, Cosmos DB, In-Memory (testing only).

Workflows:

  • Code-first — write entity classes, generate the schema with migrations (most common for new apps).
  • Database-first — scaffold classes from an existing DB (dotnet ef dbcontext scaffold).

EF6 (Entity Framework 6) is the older .NET Framework ORM, now in maintenance; EF Core is the modern rewrite (and EF6 also runs on .NET Core 3.1+ for migration scenarios).

Versions & history
VersionYearHighlights
EF 1.0 → EF 62008 → 2013.NET Framework ORM; EDMX designer, Code First (4.1), migrations (4.3)
EF Core 1.02016Rewrite, lightweight, cross-platform
EF Core 2.0/2.12017/2018Global query filters, owned types, lazy loading proxies, value conversions
EF Core 3.0/3.1 (LTS)2019LINQ overhaul — no more silent client evaluation
EF Core 52020Many-to-many without join entity, split queries, filtered Include, ToQueryString()
EF Core 6 (LTS)2021Temporal tables, compiled models, migration bundles, pre-convention config
EF Core 72022ExecuteUpdate/ExecuteDelete (bulk), JSON columns, TPC mapping
EF Core 8 (LTS)2023Complex types (value objects), primitive collections, raw SQL for unmapped types, HierarchyId
EF Core 92024Better Cosmos DB, auto-compiled models for AOT (experimental), UseSeeding
EF Core 10 (LTS)2025Ships with .NET 10: named query filters, LeftJoin/RightJoin translation, vector search for SQL Server, complex types improvements

EF Core majors follow .NET's yearly November releases and support policy (even = LTS).

Lifecycle

DbContext lifecycle (ASP.NET Core): registered scoped → one instance per HTTP request → created via DI → used for queries and changes → SaveChanges() → disposed at the end of the request. A DbContext is a unit of work, not thread-safe, and meant to be short-lived.

Entity states (change tracking):

StateMeaningOn SaveChanges
DetachedNot trackednothing
AddedNew, Add() calledINSERT
UnchangedLoaded, not modifiednothing
ModifiedProperty changedUPDATE (changed columns)
DeletedRemove() calledDELETE

Query lifecycle: LINQ → expression tree → EF query pipeline (translation, cached compiled query) → SQL → DbDataReader → materialise entities → identity resolution (one instance per key per context) → tracked as Unchanged (unless AsNoTracking).

SaveChanges lifecycle: DetectChanges() (snapshot comparison) → order commands by dependencies → begin transaction (implicit) → batch INSERT/UPDATE/DELETE → read back generated values (identity, rowversion) → commit → states become Unchanged.

Migration lifecycle: change model → dotnet ef migrations add Name (generates Up/Down C#) → review → dotnet ef database update in dev → in prod apply via SQL script (migrations script --idempotent) or migration bundle in CI/CD.

Tools used to develop
ToolUse
dotnet-ef CLI (dotnet tool install --global dotnet-ef)migrations, scaffold, bundles, dbcontext optimize
Package Manager Console (VS)Add-Migration, Update-Database, Scaffold-DbContext
EF Core Power Tools (VS extension)Reverse engineer, diagram the model
SQL Server Profiler / Extended Events / Query StoreSee and tune generated SQL
LogTo, EnableSensitiveDataLogging (dev), ToQueryString()Debug queries
MiniProfiler, Application InsightsQuery timing in apps
EFCore.BulkExtensions, Z.EntityFramework.ExtensionsThird-party bulk operations
Testcontainers, SQLiteIntegration tests