Step 1: Create a New Project
Open Visual Studio 2022.
Click Create a new project.
Select ASP.NET Core Web App (Model-View-Controller) → Next.
Name it ProductCRUD → Choose a location → Next.
Select .NET 7 or .NET 8 (latest installed) → Create.
Leave authentication as None for simplicity → Create.
Step 2: Install EF Core
Right-click your project in Solution Explorer → Manage NuGet Packages.
Install:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Step 3: Create the Model
Right-click Models → Add → Class → Name it Product.cs.
Add:
using System.ComponentModel.DataAnnotations;
namespace ProductCRUD.Models
{
public class Product
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Range(0, 10000)]
public decimal Price { get; set; }
}
}Step 4: Create the Database Context
Right-click Data (create folder if needed) → Add → Class → ApplicationDbContext.cs.
Add:
using Microsoft.EntityFrameworkCore;
using ProductCRUD.Models;
namespace ProductCRUD.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
}Step 5: Configure the Database
Open appsettings.json and add the connection string:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ProductDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}Open Program.cs and add:
using Microsoft.EntityFrameworkCore;
using ProductCRUD.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Products}/{action=Index}/{id?}");
app.Run();Step 6: Scaffold the Controller and Views
Right-click Controllers → Add → Controller.
Choose MVC Controller with views, using Entity Framework → Add.
Set:
Model class: Product
Data context: ApplicationDbContext
Controller name: ProductsController
Click Add.
Visual Studio will automatically generate all CRUD views and the controller.
Step 7: Create Database
Open Package Manager Console (Tools → NuGet Package Manager → Package Manager Console).
Run:
Add-Migration InitialCreate
Update-DatabaseThis creates the database ProductDB in LocalDB.
Step 8: Run the App
Press F5 → Your browser will open.
Go to /Products → You now have a full CRUD interface:
Create new product
Read list of products
Edit existing product
Delete product
Additional Commands
Add-Migration InitialCreate
Update-DatabaseViews/Products/Create.cshtml
@model ProductCRUD.Models.Product
@{
ViewData["Title"] = "Create Product";
}
<h1>Create Product</h1>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Create</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}Views/Products/Edit.cshtml
@model ProductCRUD.Models.Product
@{
ViewData["Title"] = "Edit Product";
}
<h1>Edit Product</h1>
<form asp-action="Edit" method="post">
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}Views/Products/Delete.cshtml
@model ProductCRUD.Models.Product
@{
ViewData["Title"] = "Delete Product";
}
<h1>Delete Product</h1>
<h3>Are you sure you want to delete this product?</h3>
<div>
<h4>@Model.Name</h4>
<p>Price: @Model.Price</p>
</div>
<form asp-action="DeleteConfirmed" method="post">
<input type="hidden" asp-for="Id" />
<button type="submit" class="btn btn-danger">Delete</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>Summary
This article walks through building a complete CRUD application using ASP.NET Core MVC and Entity Framework Core, starting from project creation to database setup and UI scaffolding. It demonstrates how to define models, configure DbContext, run migrations, and generate views, resulting in a fully functional product management system.
Join the conversation! Your thoughts help the community grow.