Featured Article

Build Complete ASP NET Core CRUD Operations Example with Entity Framework Code First

Kenneth Jul 13, 2026

Embarking on a journey into ASP.NET Core and Entity Framework? The CRUD operationsโ€”Create, Read, Update, and Deleteโ€”are the fundamentals of interacting with a database in your application. This comprehensive guide walks you through a practical example of implementing CRUD operations using ASP.NET Core and Entity Framework Code First.

the asp net core info sheet shows what it is like to work on an application
the asp net core info sheet shows what it is like to work on an application

Before we dive into the nitty-gritty, let's ensure you have both ASP.NET Core and Entity Framework Core installed in your environment. If you're using the .NET Core CLI, you can add the necessary packages to your project using the following commands:

CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane

Getting Started: Setup and Project Configuration

The first step is to create a new ASP.NET Core MVC project and install the required packages. In the terminal, run:

Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane

```bash dotnet new mvc -n CrudExample cd CrudExample dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore Designer ```

These commands create a new MVC project called "CrudExample" and add the necessary Entity Framework Core packages. Now, let's set up our Model class and DbContext.

Creating the Model Class

An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core

For this example, we'll use a simple `Student` model. Create a new class named `Student.cs` in the `Models` folder.

```csharp namespace CrudExample.Models { public class Student { public int Id { get; set; } public string Name { get; set; } public string Major { get; set; } } } ```

Setting Up DbContext

Next, create a new class named `ApplicationDbContext.cs` in the `Data` folder. This class will inherit from `DbContext` and contain a `DbSet` property for our `Student` model.

an image of a computer screen with text and numbers on it, including the same language as
an image of a computer screen with text and numbers on it, including the same language as

```csharp using CrudExample.Models; using Microsoft.EntityFrameworkCore; namespace CrudExample.Data { public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet Students { get; set; } } } ```

Now that we have our model and DbContext set up, let's configure the database connection in the `Startup.cs` file.

Database Configuration and Migrations

Open the `Startup.cs` file and locate the `ConfigureServices` method. Here, we'll configure the database connection and enable Entity Framework Core to use migrations.

๐—”๐—ฟ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐˜€๐˜๐—ฟ๐˜‚๐—ด๐—ด๐—น๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐—”๐˜‚๐˜๐—ต๐—ฒ๐—ป๐˜๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฎ๐—ป๐—ฑ ๐—”๐˜‚๐˜๐—ต๐—ผ๐—ฟ๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ถ๐—ป ๐—”๐—ฆ๐—ฃ .๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ? This guide helped a lot of developers Securing yourโ€ฆ | Anton Martyniuk | 41 comments
๐—”๐—ฟ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐˜€๐˜๐—ฟ๐˜‚๐—ด๐—ด๐—น๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐—”๐˜‚๐˜๐—ต๐—ฒ๐—ป๐˜๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฎ๐—ป๐—ฑ ๐—”๐˜‚๐˜๐—ต๐—ผ๐—ฟ๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ถ๐—ป ๐—”๐—ฆ๐—ฃ .๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ? This guide helped a lot of developers Securing yourโ€ฆ | Anton Martyniuk | 41 comments

First, add the following using directive at the top of the file:

```csharp using CrudExample.Data; using Microsoft.EntityFrameworkCore; ```

Then, modify the `ConfigureServices` method as follows:

Microsoft Access tips: Crosstab query tips
Microsoft Access tips: Crosstab query tips
Tutorial - Creating a Contact Management Database (CRM) using Microsoft Access
Tutorial - Creating a Contact Management Database (CRM) using Microsoft Access
How to Create a Company in SAP & Assign Company Code
How to Create a Company in SAP & Assign Company Code
Must Know Algorithms for Coding Interviews
Must Know Algorithms for Coding Interviews
an organization diagram with several different types of organizational roles
an organization diagram with several different types of organizational roles
cant see
cant see
an image of a computer screen with many lines and numbers on the bottom right corner
an image of a computer screen with many lines and numbers on the bottom right corner
an image of a screen with the words csrf protection on it and green text
an image of a screen with the words csrf protection on it and green text
the organizational structure of an organization is shown in blue and green, with white letters on it
the organizational structure of an organization is shown in blue and green, with white letters on it

```csharp public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddControllersWithViews(); } ```

Make sure to replace the connection string with your actual database connection string in the `appsettings.json` file. Now, let's create a migration and update the database.

Using Migrations to Create the Database Schema

In the terminal, navigate to your project directory and run the following commands to create and apply migrations:

```bash dotnet ef migrations add InitialCreate dotnet ef database update ```

These commands create a new migration with the name "InitialCreate" and update the database based on the migrations previously created. Now we have our database schema ready for CRUD operations.

Implementing CRUD Operations

ASP.NET Core provides built-in scaffolding for CRUD operations using Controllers. We'll use this feature to generate the CRUD controllers for our `Student` model. In the terminal, run:

```bash dotnet aspnet-codegenerator controller -m Student -dc ApplicationDbContext -udl -n StudentsController ```

This command generates a CRUD controller for the `Student` model using the `ApplicationDbContext` as the data context. Let's explore each CRUD operation in the generated `StudentsController.cs` file.

Create: Adding a New Student

The `Create` action in the controller is responsible for handling the creation of a new `Student`. The action receives a `Student` object as a model, populates the view with the object, and handles the POST request to insert the new student into the database.

To test the creation of a new student, run your application and navigate to the "Students" > "Create" page. Fill in the required fields and submit the form to see the new student added to the database.

Read: Displaying List of Students

The `Index` action in the controller retrieves a list of `Student` objects from the database and passes it to the view for rendering. You can see this in action by navigating to the "Students" > "Index" page in your running application.

The view also includes a search box that filters the list of students based on the entered name. This is an example of how to implement sorting and filtering functionality in your ASP.NET Core application.

Update: Editing an Existing Student

The `Edit` action in the controller enables editing of an existing `Student`. The action retrieves the student data from the database, populates the view with the data, and handles the POST request to update the student in the database.

To test the editing functionality, run your application, navigate to the "Students" > "Index" page, and click on the "Edit" link next to any student. Fill in the updated information and submit the form to see the changes saved in the database.

Delete: Removing a Student

The `Delete` action in the controller handles the removal of a `Student` from the database. The action receives the student ID as a parameter, retrieves the student data from the database, passes it to the view for confirmation, and handles the POST request to delete the student.

To test the deletion functionality, run your application, navigate to the "Students" > "Index" page, and click on the "Delete" link next to any student. Confirm the deletion and refresh the index page to see the student removed from the list.

Congratulations! You've successfully implemented CRUD operations using ASP.NET Core and Entity Framework Code First. This hands-on example covers the basic CRUD functionality, but keep in mind that real-world applications may require more advanced features, such as security, validation, and error handling.

Embrace the power of ASP.NET Core and Entity Framework Core to build efficient, data-driven web applications. Happy coding!