Database First Approach.

In this tutorial will see how to create model from database in entity framework core and will see implementation database first approach in entity framework core.

The Database First Approach creates the entity framework from an existing database. We use all other functionalities, such as the model/database sync and the code generation, in the same way we used them in the Model First approach.

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.

So lets create database and table require.

We have a simple database created in the previous article, and it contains two tables student and StudentAddress


Now we need to run the scaffold-dbcontext command with just the required parameters.

PM> Scaffold-DbContext -Provider Microsoft.EntityFrameworkCore.SqlServer -Connection "Data Source=LAPTOP-Q563PLL3\SQLEXPRESS;Initial Catalog=StudentMaster;Integrated Security=true; -OutputDir Entity -Force"

The provider and connection parameters are required, the provider is Microsoft.EntityFrameworkCore.SqlServer and the connection is the connection string for our legacy database.

When you run this command, a bunch of new files is added in the Solution Explorer

You can watch our video version of this tutorial with step by step explanation.