Introduction:
In Entity Framework Core we use to create an entity and DbContext classes for the database tables. But think about a certain scenario where you already have an existing database with the tables and for that you need to create an entity and DbContext classes for the data persistence, so probably we need do a reverse engineering with EF Core.
There are two way where you can perform reverse engineering in EF Core.
- Using the command Scaffold-DBContext or dotnet ef dbcontext scaffold.
- EF Core Power Tools
In this blog you will learn about how to do the reverse engineering using EF Core Power tools and the dot net command.
EF Core Power Tools:
Step 1: Download the EFCore power tool from visual studio marketplace.

Click here to download.
Step 2: Go to Visual studio installer and make sure the DGML Editor is installed as an individual component.

Step 3: For a demo I have created a new .NET Standard Class Library project

Step 4: Right click on newly created class library project and select EF Core power tools -> Reverse Engineer.

Step 5: Next, select the database connection from the connection wizard and click on OK.

Step 6: From the Tables selection wizard, select the tables and click on OK.

Step 7: The Generate EF Core Model window will give you the controls to play with Model classes which is auto generated with the EF Core Power tools.

- Context Name- > Provide the DbContext class name.
- Namespace -> Provide the namespace to be used for the generated code
- EntityTypes and DbContext Sub-namespace is used to provide the custom sub-namespaces for the Entity and DbContext class.
- You can select what to generate whether EntityTypes & DbContext, or only EntityTypes or the other one.
- Naming,
- Enable Pluralize or Signularize generated object name to create an entity (POCO) class names singular and DbSet name as Plural.
- Enable use table and column name directly from the database to create an entity (POCO) class and property name same as table and column name respectively.
- In other options, install the EF Core provider package in the project is enabled by default.
Step 8: After configuring the entity and DbContext classes, click on ok to generate the entity and DbContext classes.

The above image shows the list of entity classes and Dbcontext class created using EF Core power tools.
Reverse Engineering using Command
The below command is used to generate the entity classes and DbContext class based on the connection.
Scaffold-DbContext “Server=[Provide your SQL Server Name];Database= [Database Name];Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models.

I personally like to work with EF power tools to scaffold the DbContext, because it has lot of advantages over than using the commands to generate the entity and DbContext clasees. Some of them are,
- By default, it will install the EF Core libraries while doing scaffolding.
- The Interface will give you a flexibility to play with entity (POCO) and DbContext classes.
Summary:
We learned,
- Reverse Engineering EF Core using EF Core Power tools.
- Reverse Engineering EF Core using dot net core Command.
- The advantage of using EF Core Power tools over than a Command.
I hope you have enjoyed this blog. Your valuable feedback, questions, or comments about this blog are always welcome.
Reference:
https://github.com/ErikEJ/EFCorePowerTools/wiki