liveBook · Manning

Object-relational mapper

As explained in Building Web APIs with ASP.NET Core

An object-relational mapper is a software component that maps programming-language objects and operations to relational database structures and SQL operations.

An object-relational mapper (ORM) connects programming-language objects with relational database structures and operations. It lets an ASP.NET Core application work with database data through C# abstractions instead of writing every SQL query manually. section 4.0 section 4.3

An ORM is useful because it can:

  • Reduce development time and the amount of data-access code. section 4.3.1
  • Standardize retrieval strategies and reduce recurring implementation mistakes. section 4.3.1
  • Expose features such as transactions, pooling, migrations, seeds, streams, and security measures. section 4.3.1
  • Reduce manually assembled dynamic SQL and the associated SQL-injection risks. section 4.3.1

EF Core supplies the ORM layer. Its data model consists of C# classes representing database tables, while ApplicationDbContext provides the operational connection between ASP.NET Core and the database. Configure the context with a connection string, then use entities for CRUD operations and migrations to create the database structure from the model. section 4.5

Updating data through the ORM

Instead of constructing an UPDATE statement by interpolating a value into SQL, retrieve the user through DbContext, assign a new Notes value, and call SaveChanges(). The ORM presents the operation through C# while handling the database interaction. section 4.3.1

var user = DbContext.Users.Where(u => u.Id == 4);
user.Notes = "this user has been disabled";
DbContext.SaveChanges();

Compare a C# entity update with the raw SQL and parameterized alternatives to see how the ORM changes the data-access abstraction. section 4.3.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains object-schema mapping, ORM benefits, injection risks, context setup, entities, CRUD operations, and migrations.
Examples
Uses a user-notes update to contrast raw SQL, interpolated SQL, parameterized SQL, and an ORM-based C# update.
What it assumes
Assumes familiarity with relational data and C# while defining database schemas, data models, and ORM terminology.
What you can do after
Operate/configure EF Core with a context, connection string, entity classes, CRUD operations, and migrations.
How it connects
Connects relational databases, SQL, C#, EF Core, ApplicationDbContext, entities, CRUD operations, connection strings, and migrations.

Also explained in

Compare how 7 books explain object-relational mapper →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.