ASP.Net Core MVC (.Net 6.0) CRUD Operation

Introduction

We will learn CRUD (Create, Read, Update, and Delete) operations in ASP.NET Core MVC (.Net 6.0). We will use Entity Framework Core 6 to interact with the SQL Server database. We will use two Entity for CRUD operation.

Cover the following points in this blog.

  • Create ASP.NET Core MVC 6.0 project in visual studio 2022
  • Install all the necessary packages from NuGet.
  • Create entities in the Model folder.
  • Crete ApplicationDbContext class to interact with Database
  • Add connection string of SQL Server database into appsettings.json file
  • Run migration commands inside Package Manager Console (PMC)
    • Add-Migration "Initial"
    • Update-Database
  • Add controllers inside the controller folder
  • Add view models
  • Add Views
  • Add Validation

Prerequisites

  • Visual Studio 2022 (.Net 6.0)
  • SQL Server

Step 1. Create ASP.NET Core MVC 6 project

First, open Visual Studio 2022 and click on "Create a New Project" (highlighted in yellow color)

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Search "MVC" in the Search box (Search for templates (Alt+s)) and select ASP.NET Core Web App (Model-View-Controller) as a project template and click the Next button.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Enter a project name and click next.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Go with the basic configuration as per the below screen; click Next.

Asp.Net Core MVC (.Net 6.0) CRUD Operation 

ASP.NET Core MVC (.Net 6.0) project structure

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 2. Install the required package from NuGet

Required package for ASP.NET Core MVC (.Net 6.0) CRUD operation application

  • Microsoft.EntityFrameworkCore 
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

Now, we install the above package one by one. Right-click on Dependencies and select "Manage NuGet Packages…"

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Install Microsoft.EntityFrameworkCore package.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Install Microsoft.EntityFrameworkCore.SqlServer package.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Install Microsoft.EntityFrameworkCore.Tools package.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

All install packages are showing in Dependencies -> Packages

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 3. Add Category and Product entities class into the Model folder

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 4. Add ApplicationDbContext class inside the Data folder

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 5. Add connectionstring into appsettings.json

"ConnectionStrings": {
    "MVC6CrudConnetionString": "Data Source=DESKTOP-LOPK1F2;Integrated Security=true;Initial Catalog=MVC6CrudDb;TrustServerCertificate=True;MultipleActiveResultSets=true;"
}

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 6. Add DbContext Service into Program.cs file with the connection string 

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Build solution

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 7. Run migration commands inside Package Manager Console (PMC) to create a Database

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Package Manager Console (PMC)

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Add-Migration "Initial" command

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Update-Database command

Asp.Net Core MVC (.Net 6.0) CRUD Operation

MVC6CrudDb created into SQL Server with Categories and Products table.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Step 8. Add the Category controller and Product controller in the controller folder

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

In the same way, we added a Product controller, and now both are showing in the controller folder.

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Category CRUD operation screens

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Product CRUD operation screens

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation

Asp.Net Core MVC (.Net 6.0) CRUD Operation