Home > DeveloperSection > Articles > Fetch Data from Entity Framework Using ASP.NET MVC4

Fetch Data from Entity Framework Using ASP.NET MVC4


ASP.NET MVC ASP.NET MVC 
Ratings:
0 Comment(s)
 12972  View(s)
Rate this:

Fetch Data from Entity Framework Using ASP.NET MVC4

In this article I am trying to create an ASP.NET MVC4 application which is fetch the data using Entity Frame Work.

Now firstly we will create a table:

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

After that we will create a MVC4 application in Visual Studio. We will create a simple application.

After creating an application we will add an Entity Framework 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

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

a.       Select Data from left side pane.

b.      Select the ADO.NET Entity Data Model.

c.       Give the name of your Entity Framework.

d.      After that press OK.

Fetch Data from Entity Framework Using ASP.NET MVC4

3)      Then press Next.

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

a)      Click on New Connection.

b)      Give Your Server Name.

c)       Select Your Authentication, if you select Windows authentication give the User name and Password.

d)      Select your appropriate database.

e)      Press OK.

Fetch Data from Entity Framework Using ASP.NET MVC4

5)      In Fifth step we can follow below steps:

a)      Select the yes, include the sensitive data in the connection string. radio button.

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

c)       Press Next.

Fetch Data from Entity Framework Using ASP.NET MVC4

6)      In the Sixth step we can follow 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

 

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

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


Don't want to miss updates? Please click the below button!

Follow MindStick