Entity Framework Core 3.1 – Reverse Engineering

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.

  1. Using the command Scaffold-DBContext or dotnet ef dbcontext scaffold.
  2. 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.

EF Core Power Tools
Figure 1: EF Core Power Tools

Click here to download.

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

DGMK Editor
Figure 2: DGML Editor

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

Class Library
Figure 3: Class Library Project Template

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

Reverse Engineer
Figure 4: Apply Reverse Engineer

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

Figure 5: Database Connection

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

Figure 6: Select Tables

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.

Figure 7: EF Core Model
  • 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.

Figure 8: 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.

Figure 9: Package manager console

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,

  1. By default, it will install the EF Core libraries while doing scaffolding.
  2. The Interface will give you a flexibility to play with entity (POCO) and DbContext classes.

Summary:

We learned,

  1. Reverse Engineering EF Core using EF Core Power tools.
  2. Reverse Engineering EF Core using dot net core Command.
  3. 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

gowthamk91

Leave a Reply

%d bloggers like this: