How to Reverse Engineer a SQL Server Database into Entity Framework Core
By Simon Hughes, creator of the EntityFramework Reverse POCO Generator and a software developer with 35+ years of experience.
If you already have a SQL Server database and want to use it with Entity Framework Core, you
need to reverse engineer that database into C# code - the POCO entity classes,
the DbContext and the mappings that EF Core uses to talk to your tables. The
EntityFramework Reverse POCO Generator does that
in seconds: tables, views, stored procedures and functions, filtered down to exactly what your
project needs, in clean, fully customisable code. This guide shows how, and how it compares with
EF Core's built-in Scaffold-DbContext command.
The free trial generates up to 10 tables and 10 stored procedures with no time limit.
What does reverse engineering a database mean?
Reverse engineering a database is the process of reading the schema of an existing database - its tables, columns, primary and foreign keys, views, stored procedures and functions - and generating the application code that maps to it. With Entity Framework Core that means producing:
- POCO entity classes (Plain Old CLR Objects) that represent each table or view.
- A
DbContextexposing aDbSetfor each entity. - Fluent API configuration (mappings) describing keys, relationships, column types and constraints.
This process is known as Entity Framework Core reverse engineering (or EF Core scaffolding). Instead of writing all of that by hand for a database that might contain hundreds of tables, you generate it in seconds and keep it in sync as the schema evolves.
Database-first vs code-first
Code-first means you write your C# classes first and let EF Core create the database from them via migrations. Database-first is the opposite: the database already exists (often owned by a DBA, a legacy application, or another team) and you generate the C# model from it. Reverse engineering is how you do database-first development in EF Core.
Database-first is the right choice when the database is the source of truth - for example a mature production schema, a third-party database, or a data warehouse you don't control.
Reverse engineering with the EntityFramework Reverse POCO Generator
The EntityFramework Reverse POCO Generator is a
mature tool (over 575,000 downloads) built specifically for reverse engineering a
database into Entity Framework. It can reverse engineer database tables, views, stored procedures
and functions in a single pass, generating code that reads as though an expert wrote it by
hand - and gives you control that EF Core's built-in
Scaffold-DbContext command does not:
- Filter tables, views, columns, stored procedures and functions down to exactly the set you need - the key to a lean, fast
DbContext. - Stored procedures, table-valued and scalar-valued functions generated as strongly-typed methods.
- Generate C# enums from lookup tables.
- Fully customisable output through around a hundred settings, callbacks and delegates - rename, add interfaces and base classes, change conventions.
- FakeDbContext generated for fast unit testing with no database and no mocking.
- EF Core 8, 9 and 10 plus EF6, and SQL Server, PostgreSQL, MySQL, MariaDB, Oracle and SQLite.
Try it on your own database: download the free trial (up to 10 tables and 10 stored procedures, no time limit) or see pricing.
Why filtering matters: a lean DbContext starts faster
Entity Framework builds its model the first time a DbContext is used, and that cost
grows with the size of the model. Point the built-in scaffolder at a 500-table database and you
get a 500-entity context that can take several seconds to initialise on first use -
before a single query runs - because every table, column, key and relationship has to be discovered
and compiled into the model. That large model then sits in memory for the lifetime of the process.
In a long-running monolith you pay that startup cost once and move on. In microservices, AWS Lambda and Azure Functions it hurts far more: each service usually needs only a handful of tables, yet a bloated, all-encompassing context forces it to build the entire 500-table model on every cold start - adding seconds of latency to requests that should be near-instant, and wasting memory on hundreds of entities the service never touches.
Filtering is the fix
The Reverse POCO Generator lets you filter tables, views, columns, stored procedures and
functions down to the exact set each project needs. Generate a tiny, purpose-built
DbContext per microservice and it spins up in a fraction of the time and uses
far less memory. For fast, lightweight services, filtering isn't a nice-to-have - it is essential.
Want the numbers and the how-to? See the deep-dive: How to Scaffold Specific Tables in EF Core for a Faster DbContext - including a real 500-table vs 4-table benchmark (11.033s vs 0.429s) and the filter code to do it.
Step-by-step: reverse engineer your database
-
Install the
efrpgtool. It reads your database, needs the .NET 10 runtime and is installed once per machine. If Visual Studio is already open, restart it afterwards so it picks the tool up.dotnet tool install -g Efrpg - Install the extension into Visual Studio 2022 or later, from the Visual Studio Marketplace or by downloading the VSIX file and double-clicking it.
-
Add the template to your project. Right-click the project, choose Add → New Item,
search for reverse poco and name the file, for example
Northwind.tt. - Point it at your database. The connection dialog opens as soon as the file is added. Pick the database type and EF version, paste the connection string and press Test connection.
- Choose what to generate. Tick the tables, views, stored procedures and functions you need in the selection dialog. Anything you leave unticked becomes a readable filter in the template.
- Generate. The generator writes your POCO entities,
DbContext, interface, configuration mappings and aFakeDbContextalongside the template. - Save the
.ttfile whenever the schema changes to regenerate the model, keeping your data layer in sync. Building does not regenerate it; only saving does.
Your own project still needs the EF package for your database provider, for example
Microsoft.EntityFrameworkCore.SqlServer.
Full configuration options are documented in the project wiki.
The built-in alternative: Scaffold-DbContext
EF Core ships with a built-in command to scaffold a DbContext and entity types from
an existing database. From the Visual Studio Package Manager Console:
Scaffold-DbContext "Server=.;Database=Northwind;Trusted_Connection=True;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Or, using the .NET CLI:
dotnet ef dbcontext scaffold "Server=.;Database=Northwind;Trusted_Connection=True;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -o Models
This reads the schema and writes one class per table plus a DbContext into a
Models folder. For a small, simple database that is often enough to get started.
Where the built-in scaffolding falls short
Scaffold-DbContext is fine for a quick start, but on real-world databases it quickly
runs into limits:
- Clumsy filtering. The
-Tablesoption makes you list every table by name, with no pattern matching, no exclude and no column-level control - so the easy path is to scaffold everything, and a 500-table database becomes a 500-class model you have to maintain. - No stored procedures or functions. Scaffolding focuses on tables and views; stored procedures, table-valued and scalar-valued functions are not generated as strongly-typed calls.
- Hard to customise. Renaming, adding interfaces or base classes, or changing how the output is structured means hand-editing generated code that gets overwritten next time.
- Re-running overwrites your changes. Keeping the model in sync with a changing schema is painful.
- Testing is left to you. There is no fake context for unit testing without hitting a database.
The Reverse POCO Generator exists to remove each of those limits.
Frequently asked questions
Can I reverse engineer an existing database with Entity Framework Core?
Yes. EF Core supports database-first development through reverse engineering. You can use a
dedicated tool such as the Reverse POCO Generator, or the built-in Scaffold-DbContext
command, to produce the entity classes, DbContext and mappings from your schema.
Does EF Core support database first development?
Yes. Although EF Core is often used code-first, it fully supports database first development through reverse engineering. EF Core reverse engineering (scaffolding) reads your existing schema and generates the model, so you can adopt an existing database without rewriting it.
How do I generate POCO classes from a SQL Server database?
Reverse engineer the database. The generator reads each table and view and produces a clean POCO
class for it, along with a DbContext and Fluent API configuration that maps the
classes to the database.
Does this work with databases other than SQL Server?
Yes - in addition to SQL Server, the Reverse POCO Generator supports PostgreSQL, MySQL, MariaDB, Oracle and SQLite.
Does it support Entity Framework 6 as well as EF Core?
Yes. The generator supports modern Entity Framework Core (8, 9 and 10) and the older EF6, which is useful when migrating or maintaining legacy applications.