The easiest way to configure entity framework in a database first project is using EF Core Power Tools.
In Microsoft SQL Management Studio I created a database called “BugTracker” for a bug tracker project I am working on. I want to use this database with my Asp.net core MVC project so I will scaffold the database context from this database using “EF Core Power Tools”.
The project template used:
The database:
So lets begin by downloading the tool. We will need to open Visual Studio in our project, then go the following link
After downloading the tool, we will see the options the tools brings us by right clicking our project and highlighting “EF Core Power Tools”.
In this case we will be using the “Reverse Engineer” option since our project is database first and we want to create our models according to the database structure. When we click this option we will see the following modal.
In this modal we can connect to the database by clicking the “Add” button.
Here you will be able to connect to the database, in my case I have a local database so I will be using localhost with windows authentication.
In this next step we will be able to select the tables, stored procedures and views we want to include in our project.
In this last step we can choose where to write our models and where the db context will live.
Now in we will see the newly created Model in our Models folder, the DbContext and a file called “efpt.config.json”.
In our model we will see the table fields translated into C# code.
Now we can configure the context in our Startup.cs
.