ORMs, DbContext and entities
Entity Framework Core (EF Core) is a modern, lightweight, extensible, open-source and cross-platform version of the popular Entity Framework data access technology. It’s an Object-Relational Mapper (ORM) that lets .NET developers work with databases using .NET objects, and abstracts away much of the boilerplate data access code.
ORM
An ORM like EF Core bridges the impedance mismatch between the object-oriented paradigm of C# and the relational structure of databases. You define data models as C# classes (entities), and interact with the database through those objects, instead of writing raw SQL queries for common operations.
DbContext
DbContext is the class that represents a session with the database. It’s the primary API for interacting with the database. An instance of DbContext contains one or more DbSet<TEntity> properties, which represent collections of entities that can be queried from specific tables. The DbContext is responsible for tracking changesChange tracking and SaveChangesHow EF Core notices what you changed, and turns it into SQL.Opens in a new tab made to entities, and persisting those changes back to the database.
Entities
Entities are Plain Old CLR Objects (POCOs): simple C# classes that map to tables in the database. The properties of these classes typically map to columns in the corresponding tables.
Relationships
EF Core lets you define relationships between entities, such as one-to-one, one-to-many and many-to-many. You can do it with conventions, with data annotations (attributesAttributes and reflectionAttributes tag code with metadata, and reflection reads that metadata back at runtime.Opens in a new tab), or with the Fluent API (in the OnModelCreating method of the DbContext). These relationships translate into foreign key constraints in the database and navigation properties in the entity classes.
The model is built once
EF Core reflects over your entity classes once, when the DbContext is first used, to build an internal model: which properties map to which columns, which are keys, and the relationships. That includes reading attributes like the ones below. The model is cached in memory for the lifetime of the application, and every later query uses it instead of reflecting again. EF Core can also generate this model as code at build time. That is compiled modelsPooling, compiled queries and compiled modelsFour ways to cut overhead in EF Core, and two common mix-ups between them.Opens in a new tab.
public class Person{ [Key] public int Id { get; set; } [Column("full_name")] public string Name { get; set; }}Sources
- Entity Framework, Wikipedia · accessed May 16, 2025