Reverse engineering
So far, we have learned how to use EF Core to create a database schema from the entity classes. This is called code-first. However, sometimes we need to work with an existing database. In this case, we need to create the entity classes and DbContext
from the existing database schema. This is called database-first or reverse engineering. In this section, we will discuss how to use EF Core to reverse engineer the entity classes and DbContext
from an existing database schema. This is useful when we want to migrate an existing application to EF Core.
Let's use the EfCoreRelationshipsDemoDb
database as an example. If you have not created this database, please follow the steps in Chapter 6 to create it. The sample code is located at the /samples/chapter7/EfCoreReverseEngineeringDemo
folder in the chapter's GitHub repository.
- First, let us create a new web API project. Run the following command in the terminal:
dotnet new webapi -n EfCoreReverseEngineeringDemo...