Introduction
In this article, we will perform simple scaffold CRUD operations in ASP.NET Core using the EF Core Database First Approach. Creating a model from an existing database is known as the Database First Approach. Scaffolding is a technique used to generate views and controllers based on the model present in the application. Using scaffolding, you can save your time by creating CRUD operations automatically from your model. We have to perform the following steps to achieve the desired goal.
- Create SQL Table
- Create ASP.NET Core Web Application Project
- Install Required Packages
- Create Model from Existing Database
- Add Controller
- Test and run the application
Create SQL Table
Create a database named TestDB in SQL Server Management Studio and then create a table named EmployeeMaster in it which will have three columns EmployeeID, EmployeeFirstName, and EmployeeLastName. Here is a table query:
- CREATE TABLE EmployeeMaster (
- EmployeeID INT NOT NULL IDENTITY PRIMARY KEY,
- EmployeeFirstName varchar(255) NOT NULL,
- EmployeeLastName varchar(255) NOT NULL
- );
Create ASP.NET Core Web Application Project
Now we will create an ASP.NET Core Web Application project.
Step 1

Step 2

Step 3

Step 4

Install Required Nuget Packages
Select Tools menu, select NuGet Package Manager > Package Manager Console.

Install SQL Server provider by running the following command in the Package Manager Console.
- Install-Package Microsoft.EntityFrameworkCore.SqlServer
To add Entity Framework Core Tool, run the following command,
- Install-Package Microsoft.EntityFrameworkCore.Tools
Create Model from Existing Database
We will use the following Scaffold-DbContext command to create a model from our existing database
- Scaffold-DbContext "Server=DESKTOP-GV4424J;Database=TestDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
In the above command, the first parameter is connection string, the second parameter is provider name and third parameter –OutputDir is used to determine where our all classes will be generated. After running the command EmployeeMaster class and TestDBContext class (by deriving DbContext) from our TestDB will be created under the Models folder.
Add Controller
Right-click on the controller. Add->Controller. Select the MVC Controller with the views, using the entity framework.

Select EmployeeMaster class as a model class, TestDBContext as Data context class and Name Controller as EmployeeController as shown below.

After creating a controller, all CRUD operations for the controller will be automatically generated.




Mohit JoshiPosted Dec 7, 2023, 10:20 AM
InvalidOperationException: Unable to resolve service for type 'DbFirstApproach.Models.MohitContext' while attempting to activate 'DbFirstApproach.Controllers.PeopleController'. I am getting this exception after running the code. Kindly provide with solution ASAP.
Sandip NakhatePosted Sep 13, 2021, 7:24 AM
There was an error running the selected code generator install the package microsoft.visualstudio.web.codegeneration.design
arun sahaniPosted Apr 22, 2020, 11:39 AM
What about identity management ....and database for postgres