In the previous chapter, we have learnt about how we can do programming with Razor syntax. In this chapter, we are going to learn about how we can use entity framework with ASP.NET MVC.
What is Entity framework?
Entity framework is an ORM (Object Relation Mapper) Tool that provides facility to generate a data access layer from a database schema automatically. This tool comes very handy when you have schema designed for database. This reduces lots of work for the developers to write code for database layer. It can work as full fledge database layer.
ASP.NET MVC and Entity Framework:
So let us see how it works. To use an entity framework table we need to have one table in our database. So, let us first create a table called ‘Users’ as below:

Here in the above screenshot, you can see four fields UserId, UserName, FirstName, LastName and here UserId is the primary key. So now, as our table is ready, let’s put some data into it so that we can fetch that data with the help of entity framework. So, I have putted four rows for table as below:

So now, our table is ready and our data is ready. Now, it is time to create a sample ASP.NET MVC application and Entity framework classes. I am going to use existing application for ASP.NET MVC. So, now we already have MVC application ready. To create a model classes and database layer, we need to create an ADO.NET Entity model. So, point Model folder and right click Create New Item. Go to Data tab and select ADO.NET Entity Data Model as below:

Now once you click Add, it will start a wizard to create model class and the first screen will be presented as below:

Here, two options are provided, Generate from database and Empty model. However, we have our database already ready so we need to select Generate from database and once you click Next, a new screen will be provided for database connection as below:

As of now, we don’t have ready connection string. So, we have to create a new connection. So, once you click on New Connection, you will be provided with a database connection step like following:

Now, once you have your connection ready and when you press Next, ‘Choose Your Database Objects’ screen will be presented to you from where you can select your tables, stored procedures and views like following:

Now I have selected Users table and click on Finish. It will create an EDMX file like following:

Now our database layer is ready. Therefore, it’s time to create a controller file for users. So following is the user’s controller file.
using System.Linq;
using System.Web.Mvc;
using MvcApplication3.Models;
namespace MvcApplication3.Controllers
{
public class UserController : Controller
{
private TestEntityEntities db = new TestEntityEntities();
public ViewResult Index()
{
return View(db.Users.ToList());
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Here, you can see, I have Create a View Result Index Method, which will return all the users from database in a list form.
Now, once we have our database ready, It’s time to create a view for that. Therefore, I have created a view for that like following:
@model IEnumerable<MvcApplication3.Models.User>
@{
ViewBag.Title = "User View";
}
<h2>User View</h2>
<table>
<tr>
<th>
UserName
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
</tr>
}
</table>
Here ,you can see, for each model which is in our case we have users model class as defined on top, it will iterate and print username, firstname and lastname. Now, let’s run that application in browser. Once you run that in browser. It will look like following:

Here, you can see same data are there which we have inserted in our database. That’s it. You can see its very easy to use entity framework with ASP.NET MVC.
In the next chapter, we will see Scaffolding with ASP.NET MVC and we will create whole add, edit, update and delete functionality with it’s help.