How to Reverse Engineer a SQL Server Database into Entity Framework Core
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. This guide
explains database-first development, walks through the built-in Scaffold-DbContext
approach, and shows how to generate cleaner, fully customisable code with the
EntityFramework Reverse POCO Generator.
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.
Method 1: the built-in EF Core way (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:
- All or nothing. It is awkward to generate only a subset of tables, so a 500-table database produces 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.
Method 2: the EntityFramework Reverse POCO Generator
The EntityFramework Reverse POCO Generator is a mature tool (used by over 575,000 developers) 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 the built-in scaffolder 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 via flexible Mustache templates - 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, SQLite and SQL CE.
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.
Step-by-step: reverse engineer your database
- Install the extension from the Visual Studio Marketplace.
- Add the template to your project. Right-click your project, choose Add → New Item, and add the EntityFramework Reverse POCO Generator template (a
.ttfile). - Point it at your database. Set the connection string (or reference one in your config) so the generator can read the schema.
- Choose what to generate. Use the filter settings to include only the tables, views, stored procedures and functions you need.
- Run the template. Save the
.ttfile and the generator writes your POCO entities,DbContext, interface, configuration mappings and (optionally) aFakeDbContext. - Re-run whenever the schema changes to regenerate the model, keeping your data layer in sync.
Full configuration options are documented in the project wiki.
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 the
built-in Scaffold-DbContext command or a dedicated tool such as the Reverse POCO
Generator 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, SQLite and SQL CE.
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.