How to Use EF Core Power Tools to Generate EF Core Classes from Databases
Already using ASP.NET Core? Entity Framework Core? This new version of EF Core is very good, and help in the development process. Today I will talk about the EF Core Power Tools.
But how does this tool help me? With it, you can take an existing database and perform a
Reverse Engineer, which means generating all classes that you will use in Entity Framework from your database!
Remember, you need at least Visual Studio 2017 to use EF Core.
Installing the component:
Let's start by downloading EF Core Power Tools from this address: https://github.com/ErikEJ/EFCorePowerTools/wiki
TIP: At the time of writing this article, the standard version has a bug in the generation of the visual model, so I suggest you get the daily build version.
Power Tools is an extension for Visual Studio, so after downloading, click and install. Don't forget to restart Visual Studio to complete the installation!
In my case, I have Visual Studio 2017 Preview too.
Creating our sample project:
Once installed, we will create a .NET Core Console project:
Now we need to add EntityFramework Core to our project. We will do this using the NuGet Package Manager Console, with the command:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
Now we can use EF Core Power Tools by right-clicking on the project, where we will see the menu below:
We will then choose the
Reverse Engineer option, and by clicking on the
Add button you can choose the database: (in my example I will use NorthWind):
Select the tables/views and click
Finally, you can adjust your model's settings, such as Context name, Namespace, etc:
That done, we will have our classes and the context in the project:
Now use it, but first check your context, because the connection string with the database was placed there in the code (in my example NorthwindContext.cs), which is not a good practice:
A very nice feature of this tool is the database diagram, which you generate in the context menu:
This option generates a
.dgml file with the diagram:
Remembering that this tool is open source and is on GitHub: https://github.com/ErikEJ/EFCorePowerTools