100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Database

Entity Framework

By Microsoft

IntermediateFramework10.2K learners

NET that lets developers work with relational databases using strongly typed C# classes and LINQ queries instead of raw SQL.

Definition

Entity Framework (EF) is Microsoft's Object-Relational Mapping (ORM) framework for .NET that lets developers work with relational databases using strongly typed C# classes and LINQ queries instead of raw SQL.

Overview

Entity Framework maps .NET classes to database tables through a `DbContext`, which represents a session with the database and exposes `DbSet<T>` collections for each entity type. Developers can query data using LINQ — Language Integrated Query — writing expressions like `context.Users.Where(u => u.Age > 18)` that EF translates into SQL at runtime, giving compile-time checked, IntelliSense-friendly database access inside ASP.NET Core applications. The modern, cross-platform version, Entity Framework Core (EF Core), succeeded the original .NET Framework-only Entity Framework and supports PostgreSQL, MySQL, SQLite, and SQL Server through provider packages, alongside Microsoft's own Azure SQL Database. EF Core supports two primary modeling workflows: Code First, where C# classes drive the schema and migrations generate the corresponding DDL, and Database First, where an existing schema is scaffolded into C# classes. Schema evolution in EF Core is handled through its own migrations system — `dotnet ef migrations add` generates a C# migration file capturing the diff between the current model and the last snapshot, conceptually parallel to what Alembic does for SQLAlchemy or what Flyway does with SQL files, but expressed in C# and applied via `dotnet ef database update`. EF Core includes change tracking (so modified entities are detected automatically before `SaveChanges()`), lazy/eager loading of related entities, and a growing set of performance features like compiled queries and no-tracking queries for read-heavy scenarios, making it the default data-access layer for most greenfield .NET applications.

Key Features

  • DbContext and DbSet<T> model the database session and entity collections
  • LINQ-to-SQL query translation with compile-time type checking
  • Code First and Database First modeling workflows
  • C#-based migrations generated via dotnet ef migrations add
  • Automatic change tracking before SaveChanges() commits updates
  • Lazy, eager, and explicit loading strategies for related entities
  • Cross-platform, multi-database support via EF Core provider packages
  • Performance features including compiled queries and no-tracking reads

Use Cases

Mapping C# classes to relational tables in ASP.NET Core applications
Querying databases with strongly typed LINQ instead of raw SQL
Generating and applying versioned schema migrations from model changes
Scaffolding C# models from an existing database (Database First)
Tracking and persisting entity changes automatically within a request
Powering data access for enterprise .NET web APIs and services

Frequently Asked Questions