Hello Friends, In this tutorial we are going to see how to Display Records using Entity Framework in ASP.NET MVC. We are already familiar with the Entity Framework. Here we will see how we can Display Data using Entity Framework in ASP.NET MVC.

After completing this tutorial you will be able to understand:

  • How to Display Records using Entity Framework in  ASP.NET MVC.

Display Records using Entity Framework in ASP.NET MVC.

In our previous session, we have seen that how to save data using Entity Framework in ASP.NET MVC. Here we will Display the Data from SQL Server using Entity Framework in ASP.NET MVC.

Let’s Start with Creating Database and Tables in SQL Server.

Step 1 – Create Database using the following script.

CREATE DATABASE MYDB

Step 2 – Create Employee table using the following script.

USE [MYDB]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Employee](
[EmployeeID] [bigint] IDENTITY(1,1) NOT NULL,
[EmployeeName] [varchar](50) NULL,
[Gender] [varchar](10) NULL,
[Address] [varchar](50) NULL,
[State] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Status] [varchar](20) NULL,
CONSTRAINT [PK__Employee__3214EC274AA9A371] PRIMARY KEY CLUSTERED 
(
[EmployeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Step 4 – Insert some demo records into the table.

USE [MYDB]

SET IDENTITY_INSERT [dbo].[Employee] ON

GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (1, N'Vinod', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (2, N'Manoj', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (3, N'Ansh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (4, N'Ritesh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (5, N'Vishwas', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (6, N'Ansh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
SET IDENTITY_INSERT [dbo].[Employee] OFF
GO

Step 3 – Open Visual Studio and Create a ASP.NET MVC DisplayRecordsUsingEntityFramework as shown in the below image.

Display Records using Entity Framework

Step 4 – Create an Empty template and click OK.

Step 5 –  First of all, we need to Install Entity Framework. Goto Tool > Library Package Manager > Manage NuGet Packages For Solutions..

Display Records using Entity Framework

Display Records using Entity Framework

Step 6 – Right Click on the Model folder and Create a class with the name Employee and write the following code in Employee class.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;


namespace DisplayRecrodsUsingEntitryFramework.Models
{
[Table("Employee")]
public class Employee
{
[Key]
public Int64 EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Status { get; set; }


}
}

Step 7 – Right Click on the Model Folder and Create a Class EmployeeDbContext and write the following code in EmployeeDbContext class.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DisplayRecrodsUsingEntitryFramework.Models
{
public class EmployeeDbContext:DbContext
{
public DbSet<Employee> Employee { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<EmployeeDbContext>(null);
base.OnModelCreating(modelBuilder);
}
}
}

Step 9 – Add the following Connection String in web.config class with the name EmployeeDbContext.

<connectionStrings>
<add name="EmployeeDbContext" connectionString="data source=DESKTOP-9VC12LL\SQLEXPRESS;Initial Catalog=MYDB;integrated security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

Step 8 – Right Click on the Controller folder and Create a Controller with the name HomeController.

using DisplayRecordsUsingEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DisplayRecordsUsingEntityFramework.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

EmployeeDbContext objEmployeeDbContext = new EmployeeDbContext();

public ActionResult Index()
{
List<Employee> lstEmployees = objEmployeeDbContext.Employee.ToList();
return View(lstEmployees);
}

}
}

Step 9 – Right Click on the Index Action method and add a View with the name Index.

Display Records using Entity Framework

Step 10 – Write the following code to Index.cshtml file.

@model IEnumerable<DisplayRecordsUsingEntityFramework.Models.Employee>

@{
ViewBag.Title = "Index";
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="~/Css/BootstrapTableCss.css" rel="stylesheet" />
<h2>Display Records using Entity Framework</h2>
<div class="container">
<div class="row">
 
<div id="no-more-tables">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeName)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.State)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
 
</tr>

</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td data-title="Employee Name">
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td data-title="Gender">
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td data-title="Address">
@Html.DisplayFor(modelItem => item.Address)
</td>
<td data-title="State">
@Html.DisplayFor(modelItem => item.State)
</td>
<td data-title="City">
@Html.DisplayFor(modelItem => item.City)
</td>
<td data-title="Status">
@Html.DisplayFor(modelItem => item.Status)
</td>
 
</tr>
}
</tbody>
</table>
</div>
</div>
</div>

Run the Application. The output window should look like this.

Display Records using Entity Framework

View More:

Conclusion:

I hope you understand this tutorial. I would like to have your valuable comments, feedback and suggestions.

Thank You.