The "EF Core Power Tools" is an open source project on GitHub started by Erik Ejlskov Jensen. It is an extension that you can add to Visual Studio 2019. In this post I will try and introduce you to this very useful tool. We will be using the SQL-Server Northwind database running in a container for sample data.
Let's get started: https://youtu.be/FNXlsN3barQ
Running a docker container with SQL-Server Northwind sample database
I will use a docker image that contains the SQL-Server Northwind database. Credit goes to kcornwall for creating this docker image.
To pull & run the Northwind database in a docker container, run the following command in a terminal window:
docker run -d --name nw -p 1444:1433 kcornwall/sqlnorthwind
The above command does the following:
|Ports (-p):||Port 1433 in container is exposed as port 1444 on the host computer|
|Password:||The sa password is Passw0rd2018. This was determined from the Docker Hub page for the image.|
|-d:||Starts the container in detached mode|
This is what I experienced after I ran the above command:
The following confirmed to me that the container is indeed running:
Install "EF Core Power Tools" extension into Visual Studio 2019
In Visual Studio 2019, click on Extensions >> Manage Extensions.
Using EF Core Power ToolsRestart Visual Studio 2019 and open the console application that you had previously created. Now, when you right-click on the project node in "Solution Explorer", you will see "EF Core Power Tools".
|Data source:||Choose: Microsoft SQL Server (SqlClient)|
|Server name:||localhost, 1444|
|Save my password:||Checked|
|Select or enter a database name:||Choose: Northwind|
Click on OK. On the "Choose Database Connection" dialog, make sure you check "Use EF Core 5" before clicking on the OK button.
All the artifacts in the database are shown in the next dialog. These include Tables, Views, and Stored Procedures. To keep it simple, choose only the Categories & Products tables.
After selecting Categories & Products tables, click on OK. The next dialog allows you to customize the way that the reverse-engineered code gets generated in your application. I set the following values:
|Entity Types path:||Models/NW|
|DbContext path||Data||The NorthwindContext.cs file will be placed in the Data folder|
|Pluralize or singularize generated object names (English)||Checked||Entity class names will be appropriately pluralized or singularized|
|Use DataAnnotation attributes to configure the model||Checked||Data Annotations will be used instead of Fluid APIs|
|Include connection string in generated code||Checked||Connection string will be hard-coded inside the NorthwindContext.cs. Of course, this is bad practice.|
|Install the EF Core provider package in the project||Checked||The package Microsoft.EntityFrameworkCore.SqlServer be automatically installed.|
Click on OK. After the reverse engineering process is completed, you will see the following confirmation dialog:
The EF Code Power Tools are quite useful. Read more about what else you can do with it at GitHub - ErikEJ/EFCorePowerTools: Entity Framework Core Power Tools - reverse engineering, migrations and model visualization for EF Core
EF Core 5.0 diagnostics
Note the raw query at the top. This is displayed before the query is sent to the database for processing.