ASP.NET Core MVC CRUD Operations using .NET 8 and Entity Framework Core - MVC For Beginners Tutorial
Table of Contents
Introduction
In this tutorial, you will learn how to create a complete ASP.NET Core MVC application with CRUD (Create, Read, Update, Delete) operations using .NET 8 and Entity Framework Core. This beginner-friendly guide will take you through the process of setting up a student management app, which will allow you to manage student records stored in a SQL Server database. You will also gain insights into using the Model-View-Controller architecture and Bootstrap for styling your web application.
Step 1: Create a New ASP.NET Core MVC Project
- Open Visual Studio 2022.
- Create a new project by selecting "Create a new project."
- Choose "ASP.NET Core Web App (Model-View-Controller)" from the template options.
- Configure the project:
- Name the project
StudentPortal.Web
. - Set the solution name to
StudentPortal
. - Choose .NET 8 as the target framework.
- Name the project
- Click on "Create" to generate the project.
Step 2: Install Entity Framework Core Packages
- Stop the application if it's running.
- Right-click on the project in Solution Explorer and select "Manage NuGet Packages."
- In the "Browse" section, search for and install the following packages:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Step 3: Create the Application Database Context
- In the project, create a new folder named
Data
. - Right-click on the
Data
folder and add a new class calledApplicationDbContext
. - Modify the class to inherit from
DbContext
:using Microsoft.EntityFrameworkCore; public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Student> Students { get; set; } }
Step 4: Define the Student Entity
- Create a new folder called
Entities
within theModels
folder. - Add a class named
Student
to theEntities
folder:public class Student { public Guid Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public bool Subscribed { get; set; } }
Step 5: Configure the Database Connection
- Open the
appsettings.json
file and add a connection string:"ConnectionStrings": { "StudentPortal": "Server=.;Database=StudentPortalDB;Trusted_Connection=True;" }
- In
Program.cs
, inject theApplicationDbContext
:builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("StudentPortal")));
Step 6: Create the Database and Initial Migration
- Open the Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console).
- Run the following commands:
Add-Migration InitialCreate Update-Database
- This will create the database and the
Students
table.
Step 7: Create the Students Controller
- In the
Controllers
folder, add a new controller namedStudentsController
. - Define the necessary action methods for CRUD operations:
- Add: To create a new student.
- List: To display all students.
- Edit: To update a student's information.
- Delete: To remove a student.
Step 8: Implement the Add Student View
- Right-click on the
Views
folder and create a new folder namedStudents
. - Inside the
Students
folder, create a view namedAdd.cshtml
:<h1>Add Student</h1> <form asp-action="Add"> <div> <label asp-for="Name"></label> <input asp-for="Name" /> </div> <div> <label asp-for="Email"></label> <input asp-for="Email" /> </div> <div> <label asp-for="Phone"></label> <input asp-for="Phone" /> </div> <div> <label asp-for="Subscribed"></label> <input asp-for="Subscribed" type="checkbox" /> </div> <button type="submit">Save</button> </form>
Step 9: Implement the List Students View
- Create another view named
List.cshtml
in theStudents
folder:<h1>All Students</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Actions</th> </tr> </thead> <tbody> @foreach (var student in Model) { <tr> <td>@student.Id</td> <td>@student.Name</td> <td>@student.Email</td> <td> <a asp-action="Edit" asp-route-id="@student.Id">Edit</a> <a asp-action="Delete" asp-route-id="@student.Id">Delete</a> </td> </tr> } </tbody> </table>
Step 10: Implement Edit and Delete Functionality
- Add the necessary logic in the
StudentsController
to handle editing and deleting records. - Create the views
Edit.cshtml
andDelete.cshtml
to facilitate user interaction.
Conclusion
Congratulations! You have successfully created a CRUD application using ASP.NET Core MVC with Entity Framework Core and SQL Server. This tutorial covered creating a project, configuring the database context, and implementing essential CRUD operations. As a next step, you can explore adding authentication, improving UI with Bootstrap, or deploying your application to a web server. Happy coding!