Blog post image

The easiest way to configure entity framework database first in a dotnet project.

AuthorGenesis Rivera Rios
Date
8/5/2021
Time1 min read

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:

New mvc project

The database:

Database

So lets begin by downloading the tool. We will need to open Visual Studio in our project, then go the following link

Ef core tools

After downloading the tool, we will see the options the tools brings us by right clicking our project and highlighting “EF Core Power Tools”.

Ef core tools in visual studio

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.

Ef core tools reverse engineering

In this modal we can connect to the database by clicking the “Add” button.

Ef core tools reverse engineering database configuration

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.

Ef core tools reverse engineering database configuration

In this next step we will be able to select the tables, stored procedures and views we want to include in our project.

Ef core tools reverse engineering database tables

In this last step we can choose where to write our models and where the db context will live.

Ef core tools reverse engineering database tables

Now in we will see the newly created Model in our Models folder, the DbContext and a file called “efpt.config.json”.

Ef core tools reverse engineering database tables

In our model we will see the table fields translated into C# code.

Ef core tools reversed model class

Ef core tools reversed context

Now we can configure the context in our Startup.cs.

Context startup

Category: dotnetentityframeworkcsharp