Creating POCO Class Library Using Reverse Engineering

Purpose of Reverse Engineering and POCO classes

 
EF, EF Core, Dapper, or any other .NET based ORM relies heavily on C# classes to interact with the database. ORM's O means Object; i.e., C# classes which are an object-oriented representation of database objects.
 
Usually, the developers deal with an existing database of either medium or large complexity. When working with ORM's, the first task is to create C# objects. Handwritten C# objects are tedious work, leading to errors in form of names and property mismatches.
 
To avoid this, we can use reverse engineering with the database objects and convert them into C# objects. For example, the Employee table with columns can be represented as C# class Employee.cs with properties.
 
Reverse Engineering not only saves time but can maintain the true nature of the database objects. Let's get started by reverse engineering an existing database called as Northwind - a free database available online for SQL Server.
 

Install EF Core Power Tools

 
Use Visual Studio 2017 (any edition), open Tools --> Extension & Updates. Search for EF Core Power Tools under the online section, as shown here.
 
Create POCO Class Library Using Reverse Engineer
 
You would need to restart Visual Studio for changes to take effect.
 

C# POCO library

 
This can be called as Plain Old C# Objects (POCO) or entities or models etc. also.
 
Create an empty C# library (.NET Core), right-click on the project name. You will be presented with the following image. Select the "Reverse Engineer" option to generate the C# classes for the Northwind database.
 
Create POCO Class Library Using Reverse Engineer 
 

Adding connection and choosing objects

 
The Northwind database connection needs to be added so that it can reverse engineer the database objects. Here, it's present on my local machine but we can connect to any database location.
 
Create POCO Class Library Using Reverse Engineer 
 
Once the connection is added, EF Core Power Tools give us the option to select database objects (either select all or few). Only selected object's C# classes are created.
 
Create POCO Class Library Using Reverse Engineer 
 

Control over C# classes generation

 
This Power Tool gives us control on naming database context, adding namespaces and path etc. We can either generate entity types (C# classes) or DbContext (EF Core heart), or both.
 
Create POCO Class Library Using Reverse Engineer
 
I feel the POCO classes should be free from DbConext or connection string or any other packages. I just want them to be plain C# classes so that we can use them for separate validation, as View Models or as UI Models too.
 

Checking generated C# Objects

 
Once you click OK, the C# classes are generated, as shown below. These classes can be used to write the data access library, validations, with UI.
 
Create POCO Class Library Using Reverse Engineer
 
Using the EF Core Power Tools, we can get started to write the data access code quickly. Of course, we might have a situation where we need to alter those POCOs according to one's needs. We will discuss this in my next article.