In this article, we will implement CRUD Operations in Asp.Net Core MVC Application with EF Core Code First Approach.

GitHub link for this project :

Create Asp.Net Core MVC Project

In Visual Studio, Goto File > New > Project ( Ctrl + Shift + N). Then select Asp.Net Core Web Application.

In template wizard, Select Web Application( MVC ) template. Make sure to select latest Asp.Net Core Version from top dropdown. We don’t need to configure this application with HTTPS (Unchecked corresponding option from bottom).

Setup Database for EF Core

For this application development, we will use EF Core – Code First Approach. First of all, we have to install NuGet Package for EFCore. for that you can right click on project in solution explorer, click on Manage NuGet Packages. In Browse tab, search for Microsoft.EntityFrameworkCore. Install the package with same version as that of Asp.Net Core.

We will create the DBContext class inside Models folder. To demonstrate Asp.Net Core CRUD Operation, we will deal with employee details like Full Name, Employee Code,Position and Office Location. So I have named the DbContext as EmployeeContext.

Inside the class, we have a DbSet property for Employees Collection of the type Employee class. So we have to define the class with required properties as follows.

Configure the DbContext Class with Dependency Injection.

As per the DbContext class constructor parameter options of the type – DbContextOptions, we have to send DB Provider ( like for MSSQL Server / MySQL / PostgreSQL etc) and DB connection string. How can we do that ?

First of all, we will save the connection string in appsettings.json file as follows.

In order to pass both DB connection string and DB provider, we can use Dependency Injection from Asp.Net Core. For that we just need to update ConfigureServices method from Startup class. Invoke AddDbConext class from services collection as follows.

Initiate DB Migration

So far we have been modeling our Database, in-order to create the actual DB we have to initiate DB Migration. First of all open Package Manager Console. select project from Solution Explorer. Goto Tools > NuGet Package Manager > Package Manager Console.
In-order to create the Database from EF Model, you can execute following commands from Package Manager Console.

Add-Migration "InitialCreate"

After executing these two commands there will be physical DB – EmployeeDB in SQL Server Instance, which is specified in connection string.

Create MVC Controller For CRUD Operations

We have to create a new MVC controller, So right click, Controller > Add > Controller. then select ‘MVC controller with views, using Entity Framework’ template. Name the controller as EmployeeController. Select Employee class as model and EmployeeContext as DbContext class.

Created MVC controller will have all action methods for CRUD operations, even though I would like to arrange them as follows.

Inside this controller constructor, we have injected DbContext Instance from Startup Class. Index action method will fetch all employees from Employees table. Post MVC action method- AddOrEdit will do the Insert and Update Operation. Delete Operation can be handled inside Get action method Delete.

Now let’s add corresponding views. all employees retrived using index action method will be listed in index view file inside HTML table.

It will look like this.

The navbar and surrounding HTML body and header part is designed in Shared/_Layout.cshtml as follows.

@RenderSection("Scripts", required: false)

We have designed a form for Add or Edit Operation in AddOrEdit.cshml file.

CRUD Operation form look like this.

Step by Step Video Tutorial on this Topic

Make sure to subscribe our YouTube Channel – CodAffection.