Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 41 ยท Getting Started

From Objects to Tables - the Mapping Idea

Understand how an ORM maps classes to tables and properties to columns, why the object-relational impedance mismatch exists, and build a tiny reflection-based mapper in C#.

Objects and tables look similar at first glance: a class has properties, a table has columns. But they are built on different ideas. Objects hold references to other objects and form graphs; tables hold rows and connect through foreign key values. Objects have inheritance and behavior; tables have neither. This gap is known as the object-relational impedance mismatch, and bridging it is the reason ORMs exist.

Understanding the mapping idea makes every EF Core feature easier to learn, because each one is a rule in that mapping: conventions, annotations, relationships, inheritance, value converters. In this lesson you build a toy mapper with reflection, which is a close cousin of what EF Core does (EF Core compiles its mapping into optimized code, but the model is the same).

The basic correspondence

Object world Relational world
Class Table
Property Column
Object instance Row
Reference to another object Foreign key column
Collection of objects Rows in another table pointing back
null NULL

EF Core calls the classes entities and the whole description the model. The model is built when the context is first used, from conventions plus any configuration you add (Entity conventions).

flowchart LR
    C["class Book { Id, Title, Price }"] -->|"mapped to"| T["table Books (Id, Title, Price)"]
    O["new Book(...)"] -->|"one row"| R["row: 1, 'Learning EF', 24.90"]

A class maps to a table, an instance to a row.

Where the mismatch hurts

  • Identity: two objects can hold equal data and still be different objects; a row is identified by its primary key. EF Core keeps one object per key per context (identity resolution).
  • Relationships: an Order object holds a Customer reference; the table holds CustomerId. Something must keep them in sync (relationships get their own chapter later).
  • Types: decimal, DateTime, bool and enum map to database types that differ per provider (SQLite has no true decimal; SQL Server has decimal(18,2)).
  • Inheritance: tables cannot inherit. EF Core offers strategies such as one table per hierarchy.
  • Granularity: you may want one Address object but store it in the same row as the customer (Owned types).

A reflection-based mapper

The stand-in below reads a class with reflection, builds a CREATE TABLE statement and an INSERT statement, and fills objects from fake rows. It is a stand-in, not EF Core, and it ignores many details (keys, relationships, nullability).

C# Stand-in: build SQL from a class with reflection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Real EF Core adds a long list of capabilities: parameterized commands (never string concatenation), batching, provider-specific types, nullability, concurrency checks, and compiled model snapshots. Never build SQL by gluing user input into strings; the stand-in does it only to print readable text.

Identity: one object per row

A property of a good ORM session is that loading the same row twice gives the same object. The next stand-in shows a minimal identity map, a dictionary from key to object.

C# Stand-in: an identity map

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The query message appears once. In real EF Core, Find also checks the tracker first, while a normal LINQ query still hits the database but returns the already tracked instance for rows it has seen.

Mapping is a set of rules you can change

EF Core does not force one mapping. It starts with conventions, lets you override them with attributes, and then with the fluent API, which has the final say:

  1. Conventions: Id is the key, string maps to a text column, List<Order> implies a relationship.
  2. Data annotations: [Required], [MaxLength(100)], [Table("tbl_books")].
  3. Fluent API: modelBuilder.Entity<Book>().Property(b => b.Title).HasMaxLength(100).

The next chapter covers each layer in detail, starting with the DbContext and DbSet.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the object-relational impedance mismatch?

    Objects form graphs and use inheritance; tables use rows and foreign keys. An ORM bridges that gap with mapping rules.
  2. 2. In the basic mapping, what does one object instance correspond to?

    A class maps to a table, a property to a column and an instance to a row.
  3. 3. Why should real ORMs use parameters instead of building SQL by concatenation?

    Parameterized commands keep data separate from SQL text, which blocks injection and improves plan caching.
  4. 4. What does an identity map guarantee?

    Loading the same row twice in one context yields the same object, so changes are not split across copies.

Summary

  • Class = table, property = column, instance = row; references become foreign keys.
  • The impedance mismatch (identity, relationships, types, inheritance) is what an ORM manages.
  • A mapper reads class metadata (reflection or generated code) to produce SQL and objects.
  • An identity map keeps one object per key per session.
  • EF Core's mapping comes from conventions, annotations and the fluent API, in increasing priority.

Next: Providers and Connection Strings