CRUD operations using Entity Framework Code First Approach and MVC 4

CRUD operations using Entity Framework Code First Approach and MVC 4

Introduction

In this article i am going to explain about crud operations using entity framework code first approach and mvc 4. In the previous article i have explained about C# program to generate upper triangle matrixCompare Data Tables Using LINQ Union, Except and IntersectAsp.Net – How to Create Dynamic Pages & Url With C#.Net,VB.NetAsp.Net Serialization & Deserialization with C#.Net and VB.Net and How to create your first WEB API Project in ASP.Net MVC.

Explanation

Before proceeding to explain how to do CRUD operations using Entity Framework Code First Approach let us see what is meant by code first approach. Code first approch is mainly useful if you are focusing on DDD(Domain-driven design) principle. You focus on model classes and from the model class database tables will be created.

Now lets see how to do CRUD operations using Entity Framework Code First Approach and MVC 4 step by step.

Step 1 : Open visual studio and from menu choose File => New => Project and then choose ASP.NET Wep Application or ASP.NET MVC 4 Wep Application like shown in the below image.

Step 1 of CRUD operations using Entity Framework Code First Approach and MVC 4

Enter the project name and click ok. Here i name it as CRUDUsingCodeFirst as my project name.

Step 2: In the next page select Empty template and in Add folders and core references for section choose MVC as shown in the below image then click ok button.

Step 2 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 3 :  Now right click on project and choose Manage NuGet Packages option like shown in the below image.

Step 3 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 4: Now search for entity framework and install the package. Refer the below scree shot.

Step 4 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 5: After installing the entity framework reference right click on the Model=>Add => Class and name the class as Employee.cs

Step 4 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 6: Now add the below code in the employee.cs class.

 

Step 7: After creating Employee.cs class we need to create context class. Add a new class inside model folder and name it as EmployeeContext.cs Below is the code for EmployeeContext class.

In the above code MySqlConnection is the connection string name. We need to add connection string in the web.config and use that name in the constructor. Below is the connection string used in this project.

Step 8: Now we need to add controller for doing crud operations. Right click on controller folder and then choose Add and then Controller option. Please refer below screenshot.

Step 8 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 9: And then choose Controller with views,using Entity Framework option like shown in the below image and click add.

Step 9 of CRUD operations using Entity Framework Code First Approach and MVC 4

Step 10 : Now choose the Employee model in model class option and EmployeeContext in data context class option. If you want your controller actions to be async then check the Use async controller actions checkbox. And finally click ok button.

Step 10 of CRUD operations using Entity Framework Code First Approach and MVC 4

It will add all the controller actions and views for crud operations. Now if you go to EmployeesController.cs then you will find the code for all the crud operations.

And go to the views folder you can view all the necessary views added. Now if you run the application you can see the below output.

Step 11 of CRUD operations using Entity Framework Code First Approach and MVC 4

Click on Create New link. It will open the page to add new employee like below.

Step 12 of CRUD operations using Entity Framework Code First Approach and MVC 4

If you click on the Details link you will see the below page.

Step 13 of CRUD operations using Entity Framework Code First Approach and MVC 4

That’s it.Hope you enjoyed to learn how to implement crud operations using entity framework code first approach and mvc. Please share your feedback.

 

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!