CRUD operations using Entity Framework Database First Approach and MVC 4

Introduction

In this tutorial i am going to explain about how to do crud operations using entity framework database first approach and MVC 4. In my previous article i have explained about CRUD operations using Entity Framework Code First Approach and MVC 4,Convert Dataset Datatable To Json Data Array in Asp.Net C#.Net & VB.Net or How To Use JavaScriptSerializer to Convert Datatable to Json Data ArrayASP.Net Convert XML to Datatable in C#.net, VB.Net and C# program to generate upper triangle matrix

CRUD operations using Entity Framework Database First Approach and MVC 4

Explanation

Before we see how to do crud operations using entity framework database first approach and MVC 4 we should know what is meant by Database First approach. Database first approach can be adopted if we have already have database and tables created then we can use this to create our models. Now we will see how to do CRUD operations using Entity Framework Database First Approach and MVC 4.

Step 1 : Open Visual studio and go to file menu choose New = > Project and then choose Asp.Net Web Application template, enter the project name here i have entered it as CRUDUsingDatabaseFirst then click ok as shown in the below image.

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

Step 2 : Now in the next page choose Empty from select a template section and MVC from add folders and core references section then click ok as shown below.

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

Now open the sql server and create a table called student. Below is the script i used for demonstration purpose.

Step 3: Now the project will be created with specified configuration option. Now right click the project and choose Add=>New Item and then choose Ado.net Entity Data Model from available templates or you can directly select Add=> Ado.net Entity Data Model like shown in the below image.

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

It will open the below form and ask for name for item. Name it same as your database table in our case it is student.

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

Step 4: Now in the next screen Entity Data Model Wizard choose EF Designer from database option for choose model contents and click Next.

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

Step 5: It will ask you to choose your data connection. Choose your connection if it is already there in the drop down or click New Connection to add your connection string.

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

Step 6: Now if you click the new connection link it will open the below form.

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

Enter your servername and use the suitable authentication mode(here i am using windows authentication) and then choose your database where you have created the student table. Now you can check your connection using test connection button provided in the left bottom of the dialog. And then click ok to proceed.

Step 7: Now after clicking ok it will go to the previous dialog and fill the available info like shown in the below figure.

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

Click Save connection settings in Web.Config as checkbox and give the suitable name as per your need. It will add connection string in web.config.

Step 8: Then it will prompt you to choose the Entity Framework version. Choose the highest version available and click next.

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

Step 9: Now it will list all the database objects such as tables,views and stored procedures. Choose the student table which we created and click Finish button.

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

Step 10: Now you may get the security warning like shown below. Click ok button to proceed.

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

Step 11: Now if you check the project student related files will be there. Please refer the below figure.

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

If you double click the student.edmx file you can see the below entity flow diagram.

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

Now if you check the Student.cs file it will contain the below code i.e. model is auto generated from the student table.

Step 12: Now Right click on controller folder and choose Add option and select New Scaffolded Item option like shown in below image.

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

It will open the below dialog with the available templates. Choose MVC 5 Controller with views, using Entity Framework template and click Add button.

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

After that choose Student class in Model class option and for Data context class – click on + plus icon it will ask to enter the New data context type. Enter the name and click add like shown in below picture.

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

Now open the StudentContext.cs and change the connectionstring name like shown in the below file.

Now if you check the StudentsController.cs you can see the below auto generated code.

And check views folder, views are auto generated for all actions. And now run the application you will see the below output.

Output of CRUD operations using Entity Framework Database First Approach and MVC 4

Hope you have learned how to do crud operations using entity framework database first approach and mvc4. Please post your comments in the comment box.

Subscribe For Latest Updates

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