
This tutorial guides you through creating an ASP.NET Core MVC application from scratch, focusing on CRUD operations for managing student data using .NET 8 and Entity Framework Core. You'll learn to set up the project, create a SQL Server database, and implement functionalities to create, read, update, and delete student records.
In this tutorial, we will create an ASP.NET Core 8 MVC application from scratch, focusing on performing CRUD (Create, Read, Update, Delete) operations on a SQL Server database using Entity Framework Core. This guide is designed for beginners and will walk you through each step of the process.
The application we will build is a simple student management system. It will allow users to:
StudentPortal and set the location for your project files.To interact with the SQL Server database, we need to install Entity Framework Core packages:
Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.ToolsApplicationDbContext.cs.DbContext and set up the constructor to accept DbContextOptions.DbSet<Student> property to represent the students table.public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Student> Students { get; set; }
}
Student.cs with properties for ID, Name, Email, Phone, and Subscribed.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; }
}
appsettings.json and add a connection string for your SQL Server database."ConnectionStrings": {
"StudentPortal": "Server=.;Database=StudentPortalDB;Trusted_Connection=True;"
}
Program.cs, configure the services to use the ApplicationDbContext with the connection string.builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("StudentPortal")));
StudentsController.cs.<h1>Add Student</h1>
<form asp-action="Add">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Phone"></label>
<input asp-for="Phone" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Subscribed"></label>
<input asp-for="Subscribed" type="checkbox" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
<h1>All Students</h1>
<table class="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>
StudentsController, implement the logic to handle the creation, retrieval, updating, and deletion of student records using the ApplicationDbContext.Add-Migration InitialCreateUpdate-DatabaseIn this tutorial, we successfully built a beginner-friendly ASP.NET Core MVC application that performs CRUD operations using .NET 8 and Entity Framework Core. You can expand this application further by adding features such as authentication, validation, and more complex data relationships. Happy coding!
Paste a YouTube link and let Magica create the key takeaways.
Summarize another video