How to scaffold entity type classes and DbContext based on an existing database schema

Install these NuGet packages in the project:

  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFramework.SqlServer

Create a folder called “Models” at the root of the project.

Run this command in the package manager console of Visual Studio (make sure you have the correct startup project selected):

Scaffold-DbContext "Data Source=<{SERVER_NAME};Initial Catalog={DATABASE_NAME};Persist Security Info=True;User ID={USERNAME};Password={PASSWORD}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Source: https://docs.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=vs