Search⌘ K
AI Features

Scaffolding Models Using an Existing Database

Explore scaffolding in Entity Framework Core to reverse engineer database tables into C# models. Understand how to generate and customize classes without losing changes and compare autogenerated code to manual models for effective data handling.

Scaffolding model

Scaffolding is the process of using a tool to create classes that represent the model of an existing database using reverse engineering.

Scaffolding tool

A good scaffolding tool allows us to extend the automatically generated classes and then regenerate those classes without losing our extended classes. If we know that we will never regenerate the classes using the tool, feel free to change the code for the automatically generated classes as much as we want. The code generated by the tool is just the best approximation.

Note: Do not be afraid to overrule a tool when we know better.

Comparing auto-generated models with manual creation

Let’s see if the tool generates the same model as we did manually:

Step 1: Add the latest version of the Microsoft.EntityFrameworkCore.Design package to the WorkingWithEFCore project.

Step 2: At a command prompt or terminal in the WorkingWithEFCore folder, generate a model for the Categories and Products tables in a new folder named AutoGenModels, as shown in the following ...