Implement CRUD operation ASP.NET MVC with EF Core
By: Alpesh Patel
Tags: Software Engineering, Development, Aspnetcore, Software Development, Aspnet

In this tutorial we will learn how to implement CRUD (Create, Read, Update and Delete) with SQL database using EF core code first approach. You’ll review and customize the CRUD (create, read, update, delete) code that the MVC scaffolding automatically creates for you in controllers and views.

Photo by James Harrison on Unsplash

Prerequisites

The following must be installed in your system:

- Visual Studio 2019 16.4 or later with the ASP.NET and web development workload

- .NET Core 3.1 SDK or later

- Basic knowledge of Code First EF core

Step 1 New Project

Once you have installed the Visual Studio 2019 and .Net Core 3.1 SDK, you can start building a new ASP.NET Core Application.

Open VS2019

On the start page, choose Create a new project.

Create a web app and From the Visual Studio select Create a new project. Select ASP.NET Core Web Application > Next.

Give your project a name i.e. CRUDOperationASPDotNETMVCEFCore and give your project a location where it’ll be saved and click Create.

Step 2: Install the NuGet Packages

We will install some of the required Entity Framework Core packages from the NuGet Package Manager for performing database operations from the code.

From the Solution right click on dependency, select Manage NuGet Packages and Select the Browse tab, and then enter Microsoft.EntityFrameworkCore.SqlServer in the search box, select Latest Stable Version and Install.

Now, install Microsoft.EntityFrameworkCore.Tools as same as above steps.

Step 3: Create Model and Context Classes:

Now, Add new class with name “Teacher”, using Right-click the Models folder and select Add Class.

Replace the template code with the following code:

Now it is ready for add database context:

Right-click the Models folder and select Add > Class. Name the class TeacherContext and click Add and change code in TeacherContext.cs as following:

We first write the code, which means Model classes, and on the basis of these classes our tables are auto-generated inside the database. However, to create these tables in the database we have to define our connection string where we will define our server and database name.

Connection string you need to write inside the appsetting.json file as follows:

- In ASP.NET Core, services such as the DB context must be registered with the dependency injection container. The container provides the service to controllers.

Update Startup.cs with the following highlighted code:

Step 4: Create Database using Migration:

- Open Package Manager Console from the Tools Menu and select the Default project for which you would like to generate migrations code. For this demonstration, we have only a single project as CRUDOperationASPDotNETMVCEFCore. Therefore, by default, it is a default project.

For creating the migration code, we use ‘add-migration MigrationName’ command. So, let’s perform this operation and see what happens. Therefore, in the Package Manager Console, just type ‘add-migration initialmigration’ command and press Enter.

After the add migration command executes successfully, it creates a folder name as ‘Migration’ in the project and creates the class with the same name [MigrationName] as we have provided while executing add migration command with some name. Here you can see the table structure based on your Model (Teacher), which is ready to generate the database.

We have only created the migration script which is responsible for creating the database and its table. But we’ve not created the actual database and tables. So, let’s execute the migration script and generate the database and tables. Therefore, executing the migration scripts we have to execute ‘update-database’ command.

After the add update-database command executes successfully, you will received message for “Done” and you can see you database also created on SQL server.

We just need to go to SQL Server Management Studio and login and see the Database. You will find the database, table and Entity Framework Migration history table as follows.

Step 5: Adding Controller using scaffold

Create ‘Controllers’ folder on the root of the Application. We will use scaffold to add new controller as below:

From the Solution right click on controllers folder, select add -> Controller. On Add New Scaffolded Item popup select “MVC Controller with views, using entity framework” template.

After click on Add button on Add New Scaffolded popup, open one more popup and on that select model (ex. Teacher) and your data context class and click on Add button.

Now, you can see your controller and the following view pages has been added in the view folder. Create.cshtml, Edit.cshtml. Details.cshtml and Delete.cshtml

Your controller and views are created with all default View, Create, Update and Delete feature.

Now, Add new menu on header as below for teachers.

Step 6: Run Project

Let’s run the project and see the how’s work CRUD option without any written one line code.

Click on ‘Teachers’ menu on top and you can see the list of Teachers:

Now, Click on Create new button and create new teacher as below:

You can edit, view details and delete using right side buttons.

Here validation also is done by default. Let me show you.

Thank You…! :)