How to Connect ADO.NET Entity Framework With MVC in Simple Steps

This article shows how to work with MVC and the ADO.NET Entity Framework together.

Now a days most developers are moving toward MVC instead of working with webforms because of the rapid development and good testing features in MVC.

You can download the adventureworkslt2008r2 Sample Database from the following link and use it.

AdventureWorks Databases – 2012, 2008R2 and 2008

Now we have various ways to connect with a database in MVC.

  1. MVC with a Data Access Layer (DAL).

    Creating Insert Update and Delete Application In MVC 4 Using Razor
  2. MVC with Dapper, a Freeware ORM from Stackoverflow; it is one of the fastest data retrieval ORMs compared to Entity Framework and NHIBERNET.

    Performing CRUD Operation With Dapper ( Open Source ORM From Stack Overflow) in MVC
  3. Now I am showing the ADO.NET Entity Framework (.edmx) and there are many more ways I will be showing.

Now Let's start with it.

Step 1: Create a new project; choose ASP.NET MVC 4 Web Application (Visual C#).

And I am naming it ConnectwithADO and Click OK.

A new wizard will pop-up; from that just select Internet Application and View Engine choose Razor.



And after clicking OK, a default Home Controller screen will be visible.

Step 2: Adding ADO.NET Entity Framework.

Open Solution Explorer then right-click on the project (ConnectwithADO).

From the list choose Add and from the sub-list choose New Item.



After clicking New Item a new Wizard will pop-up.

From the left type Visual C# to select the data.

On Select Data you will see ADO.NET Entity Data Model and another item related to Data.

In that select ADO.NET Entity Data Model and click Add.

I am not changing the name of it for the Demo.



Step 3: Configuring ADO.NET Entity Data Model.

As soon as you click on Add a new wizard of Entity Data Model will appear.

I will choose the Generate from database option from these options and click on the Next button.



Then it will prompt for your Database Connection.



Click on New Connection; a Connection Properties Wizard will pop-up; fill in all your details.

In the following snapshot you will see where to fill in the details.


After Entering the details of Username and Password a list of databases will pop up.

Select the database, whatever you have .

But in this I will select AdventureWorksLT2008R2.

Click on Test Connection and check it then click OK.

Then you can see all Changes in the Entity Connection String that have you chosen.

You can also configure the webConfig Connection String Name.



The last option to choose is to show the sensitive data in the Connection string; click Yes.



Step 4: Select the object to use.



Select Tables and Expand it to see all the tables.

In that I will select 2 tables, customer and Product, and finally click on Finish.



After this Visual Studio will generate a database diagram for the table that we chose.



Now save your project.

In Solution Explorer you will find the model and your tables.



Add your table class will be created.



Step 5: Build the project and add the Controller for the model that we created.



After clicking on the Controller, the Add Controller Wizard will pop up.

I will name the controller TestController.



In the template select MVC Controller with read write action and views using Entity Framework.



Select the Model Class for generating your CRUD for that model.

Here I will choose Customer.



Finally I will select Data Context class that I created in Step 4.

You will also find the Data Context class in the webconfig.





After clicking add you will find all CRUD views and the Controller is generated.



Step 6: Now just Run Project.



Here all the table data for the customer has been populated in the list. It ensures that our ADO.NET is working.



Wow! We have completed the process of connecting MVC with the ADO.NET Entity Framework.

For any other articles of MVC you can visit.


Similar Articles