Asp.Net Core MVC CRUD with EF Core

Asp.Net Core MVC CRUD Operations with EF Core.

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.

Image Showing Creation of 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).

Picture showing creation MVC Web Application in .Net Core.

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.

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.

Image showing creation of MVC Controller in Asp.Net core for CRUD operations.

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.

Image Showing List of Employee which retrieved from SQL Server Database.

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

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

CRUD Operation form look like this.

Image showing design of form for insert and update operation.

Step by Step Video Tutorial on this Topic

Make sure to subscribe our YouTube Channel – CodAffection.

Leave a Reply

5 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
3 Comment authors
BenHeráclidesSurajit Santra Recent comment authors
newest oldest most voted
Notify of
Surajit Santra
Surajit Santra

Hello Sir,
I like your channel, every day I check your channel if any video comes in.

Sir, Please create a tutorial video for authentication & login with social login. Use(Facebook & Google) social login with Angular 7 & ASP.Net core.
Please share this one.



excelente tutoríal me a servido mucho
Si es posible algún ejemplo utilizando Storage Procedure con EF con una DB relacional SQL Server

le agradezco de antemano


Thanks very good. I will like to be following your tutorials


great tutorials


Great job . thanks for this simplifying post