inchirags@gmail.com Chirag's Asp.Net Core MVC Tutorial https://www.chirags.in
*********************************************************************************************
Creating CRUD (Create, Read, Update, Delete) operations in an ASP.NET Core MVC Application
*********************************************************************************************
YouTube Video:
Creating CRUD (Create, Read, Update, Delete) operations in an ASP.NET Core MVC application involves several steps. Below is a step-by-step guide for implementing CRUD operations in an ASP.NET Core MVC project:
Prerequisites
1. Visual Studio (any version supporting ASP.NET Core MVC)
2. Basic knowledge of C# and ASP.NET Core MVC
3. SQL Server Database (for database operations)
Step 1: Set Up Your ASP.NET Core MVC Project
1. Create a New ASP.NET Core MVC Project:
Open Visual Studio.
Select File > New > Project.
Choose ASP.NET Core Web App (Model-View-Controller).
Name your project and configure the settings as needed.
2. Install Necessary Packages:
Install Entity Framework Core and its tools via NuGet Package Manager > Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Step 2: Configure the Database Connection
Open SQL Server Management Studio (SSMS) or your preferred database tool.
Create a new database (e.g., CrudCoreDb).
1. Define the Connection String:
Open appsettings.json.
Add the connection string for your SQL Server database:
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=CrudCoreDb;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true"
}
2. Create the Database Context Class:
Right-click the Models folder, select Add > Class, and name the class AppDbContext.cs
Add a new class called AppDbContext:
using Microsoft.EntityFrameworkCore;
namespace WebApplication2.Models
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
}
3. Register the DbContext in Program.cs:
Add the following code in the builder.Services section:
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Step 3: Create the Model
Right-click on the Models folder and select Add > Class.
Name the class Product.cs and add the following properties:
1. Add a new class named Product:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
}
2. Add Migration and Update the Database:
Run the following commands in the Package Manager Console:
Add-Migration InitialCreate
Add-Migration CreateProductsTable
Update-Database
Step 4: Create the Controller
1. Add a Controller:
Right-click the Controllers folder > Add > Controller.
Choose MVC Controller with views, using Entity Framework.
Select the Product model and AppDbContext.
2. Visual Studio will generate:
A ProductsController class with all CRUD actions.
Views for Create, Edit, Delete, Details, and Index.
Step 5: Update the Views
Customize the generated Razor views as needed in the Views/Products folder.
Step 6: Add Navigation
Update the Shared/_Layout.cshtml file to include a link to the Products page:
<li class="nav-item">
<a class="nav-link" asp-controller="Products" asp-action="Index">Products</a>
</li>
Step 7: Run the Application
Press F5 or Ctrl+F5 to run the application.
Navigate to /Products to access the CRUD functionality.
Summary
This process includes creating a project, configuring the database, generating models, scaffolding controllers and views, and integrating everything into a functional application. With these steps, you’ll have a working CRUD application in ASP.NET Core MVC.
For any doubts and query, please write on YouTube video comments section.
Note : Follow the process shown in the video.
Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
inchirags@gmail.com
_________________________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.
#chirags
#chiragstutorial
chirags, chirags tutorial, chirags Asp.Net Core MVC tutorial