15 49.0138 8.38624 1 0 4000 1 https://codeisall.com 300
CRUD Operation in MVC

Code-First Approach With ASP.NET MVC Project

Introduction

In the previous article, you have learned about the difference between Code-First and Database-First approaches.

In this article, you will learn how to establish a SQL Server database connection to MVC 5 applications with the help of the Code First approach, and connect SQL databases to perform operations in a project.

This article will be helpful to beginners and students who are learning MVC. 

What is Code First Approach?

Code-First approach is an alternative of the database first and model first approaches to the entity data model and create a database as per the model we create.

In a simple way, Code-First we create the model classes as per the tables in the database we want to create. And in the Database-First approach, we create the database first, and then the model class of the table will be generated automatically when we .edmx file generates.

To know more about the code-first and Database-first approach visit our previous article.

Pre-requisites

  • You need to create an ASP.NET MVC Project (You can refer our article to create a new project), Or you can add this into the existing project also A SQL  server Installed where you can create a database.
  • An empty Database created on the SQL server. 

Update Empty Database with Code-First

Step 1: Add new class library to solution

As you have already a demo project created we will add a class library to the solution to separate the data logic from the project, so it gets easy to maintain in the future.

Add the class library right-click on the solution and add a new project as shown below.

You will get the window like the below image, select the class library (.NET framework), and hit the next button.

Give the appropriate name of the project as I have given here and hit the create button as shown in the image below.

Step 2: Add entity framework to the project

Go to the NuGet Package Manager Console and run the below code to install the entity framework in the project.

Step 3: Create an Entity for Employee

In the EmployeeManagement.Data project create a folder and name it “Model” and add a new class called “Employee.cs” under that folder and add the below code to the class.

After creating the class for the table, you have to create a context class in the model folder, name it “EmployeeDBContext.cs” ( In database-first approach it creates automatically when .edmx file creates). Add the below-given code in that class.

Step 4: Enable Migration

After adding the entity class and the context class very important step is to enable the migration in the project.

To enable migration open NuGet package manager console and select the project where your entity classes are held (in our case EmployeeManagement.Data) and run below code as shown.

Step 5: Add-Migration and update base

To add the migration and update the database run below command one by one and it will create one folder named “Migrations” in the project and the migration file will be in that folder.

  1. add-migration addDB
  2. update-database  

Now check in the database, there will be a table created as per the entity name we have given and also one more table with the name of “__MigrationHistory” as shown below.

Project Demo

As our main goal of creating and updating the database with the code first is achieved, we will perform a CRUD operation to get more idea about how it works

The below code will work for both: code-first and database-first approach. Replace the home controller with the given below code.

Below method is used to manage the specific employee, here passing the specific employee id and it will return the specific view with that employee.

Here is other Manage method that is post type and use for insert or update the employee data. if employee available then it will update the record otherwise it insert new data to database.

The following method is used to delete specific employee. just passing the employee id into that method that’s it.

Right-click on the Index and Manage method and add views and replace the below code.

Index.cshtml

Manage.cshtml

Now run the project and you will get the below screen. where you can add edit and delete the particular record.

Hope this article will help you to Perform code first method to create and update database using code. Comment your suggestions and issues and we will try to resolve it.

Share:
Category:MVC
PREVIOUS POST
Entity Framework : Code First Vs Database First
NEXT POST
ViewBag, ViewData, And TempData In MVC