---
title: "Fetch Data from Entity Framework Using ASP.NET MVC4"  
description: "In this article I am trying to create an ASP.NET MVC4 application which is fetch the data using Entity Frame Work."  
author: "Vijay Shukla"  
published: 2013-01-09  
updated: 2020-02-15  
canonical: https://www.mindstick.com/articles/1109/fetch-data-from-entity-framework-using-asp-dot-net-mvc4  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 4 minutes  

---

# Fetch Data from Entity Framework Using ASP.NET MVC4

## Fetch Data from Entity Framework Using ASP.NET MVC4

Today In this Article, I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to create an ASP.NET MVC4 application that fetches the data using Entity Frame Work.\

Now Firstly we will create a table - ( in SQL)

##### Table:

```
CREATE TABLE [dbo].[tblEmployee]
(
  vEmpId varchar(10)PrimaryKey,
  vEmpName varchar(100),
  vEmpAddress varchar(100),
  dEmpDOB date
)
```

##### Insert values in [dbo].[tblEmployee] table

```
INSERT INTO [dbo].[tblEmployee] VALUES ('EOO1','Vijay Shukla','1082 Rjrooppur Allahabad','2004-05-23’)
INSERT INTO [dbo].[tblEmployee] VALUES ('EOO2','Ajay Shukla','1082 Rjrooppur Allahabad','1985-01-21’)
```

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/20bac578-5e3d-4144-851f-1fd27ae9dcdd.png)

After that, we will create an MVC4 application in [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available). We will create a simple application.

After creating an application we will add an [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach) in our application.

##### Below steps for adding entity framework in our application

**1)** Right Click on Models folder which is under the Solution Explorer then go to ADD >> New Item…

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/a323de8d-5e04-4675-a186-4d52890ca25e.png)

**2)** In the second step we can follow the below steps:

a. Select Data from the left side pane.

b. Select the ADO.NET [Entity Data Model](https://www.mindstick.com/articles/594/ado-dot-net-entity-data-model-in-wpf).

c. Give the name of your Entity Framework.

d. After that press OK.

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/99e33dec-f28c-4278-bbad-544f82df6270.png)

**3)** Then press Next.

**4)** In the fourth step we can follow the below steps:

**a)** Click on New Connection.

**b)** Give Your [Server Name](https://answers.mindstick.com/qa/110129/how-to-change-discord-server-name).

**c)** Select Your [Authentication](https://www.mindstick.com/blog/177/authentication-and-authorization-in-asp-dot-net), if you select Windows authentication give the [User name](https://answers.mindstick.com/qa/30717/how-to-retrieve-user-name-in-case-of-window-authentication) and Password.

**d)** Select your appropriate database.

**e)** Press OK.

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/b8923b5f-7824-42c2-baa1-dfbb2e4c3371.png)

**5)** In Fifth step we can follow the below steps:

a) Select the yes, include the [sensitive data](https://www.mindstick.com/forum/157782/how-do-you-protect-sensitive-data-such-as-passwords-and-credit-card-information-in-an-asp-dot-net-app) in the [connection string](https://www.mindstick.com/articles/17/connection-string). [radio button](https://www.mindstick.com/articles/12743/radio-button-in-android).

b) Give the name for entity connection settings in the Web.Config file.

c) Press Next.

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/217998ce-9494-4cb3-b622-8e5f12c46fed.png)

**6)**In the Sixth step we can follow the below steps:

a) Select your table.

b) Give the name for Model namespace.

c) Press Finish button.

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/bea392f8-6a63-418c-9ba4-1e4b8fd37608.png)

After that, you can see your entity is added on your Models folder which is under the Solution Explorer.

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/ffb40f9e-d3be-49c5-b0b4-5f3056e0e931.png)

After that, we can add a Controller and view.

**Controller Code:**

```
using System; 

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FetchDataFromeDatabaseSimpleMVC4.Models; //This is Models Class Namespace
 
namespace FetchDataFromeDatabaseSimpleMVC4.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        FetchDataEntities _fetchDataEntities = new FetchDataEntities();
        public ActionResult Index()
        {
            return View(_fetchDataEntities);
        }
 
    }
}
```

Above code, I’m creating the FetchDataEntities class’ object _fetchDataEntities and this object passed in View().\

##### View Code:

```
@model FetchDataFromeDatabaseSimpleMVC4.Models.FetchDataEntities
@{
    ViewBag.Title = "Index";
}
<h2>Employee Information</h2>
<table border="1px">
    <tr>
        <th> Employee ID</th>
        <th> Employee Name</th>
        <th> Employee DOB</th>
        <th> Employee Address</th>
    </tr>
    @foreach (var item in Model.tblEmployees)
    {
        <tr>
            <td>@item.vEmpId</td>
            <td>@item.vEmpName<td>
            <td>@item.dEmpDOB<td>
            <td>@item.vEmpAddress</td>
        </tr>
   }
</table>
```

Above code, we will add the FetchDataEntities which is under the Models Class, and our table (tblEmployee) is under the FetchDataEntities.

##### Output:

![Fetch Data from Entity Framework Using ASP.NET MVC4](https://www.mindstick.com/mindstickarticle/6dfea253-4698-4eb8-ab20-57d2aa61753a/images/4f7ac403-ff36-41be-8119-874c0791a4cc.png)

---

Original Source: https://www.mindstick.com/articles/1109/fetch-data-from-entity-framework-using-asp-dot-net-mvc4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
