Database first: scaffolding an existing database
Not every project starts with an empty database. This lesson shows how to scaffold a DbContext and entity classes from an existing database, choose which tables to include, keep your own changes safe when you scaffold again, and decide whether to switch to migrations later.
- StartExisting databaseOwned by you, a DBA or another app
- Step 2Scaffolddotnet ef dbcontext scaffold writes the classes
- Step 3Add partial classesYour own code lives in separate files
- Step 4Schema changesScaffold again with --force
- EndOptional switchCreate a baseline migration and go code first
Scaffolding
The dotnet ef tool reads the database schema and writes a DbContext plus one class per table. Keep the connection string out of the command history by using a named connection from user secrets.
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet user-secrets set ConnectionStrings:Shop "Server=.;Database=Shop;Trusted_Connection=True;TrustServerCertificate=True"
dotnet ef dbcontext scaffold Name=ConnectionStrings:Shop Microsoft.EntityFrameworkCore.SqlServer \
--output-dir Data/Entities --context-dir Data --context ShopContext \
--table Products --table Categories --table Orders --no-onconfiguringUseful options
- --table or --schema limits which tables are scaffolded.
- --data-annotations uses attributes where possible instead of Fluent API.
- --no-onconfiguring keeps the connection string out of the generated class; register the context in Program.cs instead.
- --force overwrites files when you scaffold again after a schema change.
- --use-database-names keeps names exactly as in the database.
Keeping your own code
Generated classes are partial. Put your additions in a separate file so re-scaffolding with --force does not wipe them.
// Data/Entities/Product.Custom.cs (never overwritten)
public partial class Product
{
public bool IsCheap => Price < 100;
}
// Data/ShopContext.Custom.cs
public partial class ShopContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasQueryFilter(p => !p.IsDeleted);
}
}Database first or code first from here?
If a DBA or another team owns the schema, stay database first: they change the database and you scaffold again.
If your team now owns the schema, you can switch to migrations: create an initial migration, then empty its Up method (the tables already exist) or mark it applied, and make every later change in code.
Interview question
How do you keep custom code when re-scaffolding an EF Core model?
Scaffolded entity classes and the DbContext are partial, so custom properties and methods go in separate partial class files that the tool never touches. Extra model configuration goes in the OnModelCreatingPartial method, also implemented in a separate file. Then dotnet ef dbcontext scaffold --force can regenerate the generated files safely.
Want the full details? Read the official Microsoft documentation for this topic.