Using Entity Framework in Asp.Net MVC 4 with Example

Entity Framework is an Object Relational Mapper (ORM). This ORM provides developer to automate mechanism of storing & accessing the data from database.


Most developers use traditional way of data access using ADO.NET framework by writing Data access layer class for performing CRUD (Create, Read, Update, Delete) operations and these processes are time consuming because we need to write custom queries for all these operations and then need to call them by creating object of classes and dispose them properly. Microsoft came up with new data accessing mechanism called as "Entity Framework”.


Now let’s have a look on the standard definition of Entity Framework given by Microsoft:


“The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework’s ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.”


We can use "Entity Framework" in three different way


  1. Database first
  2. Code first
  3. Model first

Database first

In Database First approach, we use existing database to create business model from that. For more info check this Database Frist Approach in Entity Framework

Code first

In Code first approach, we will create POCO classes first and according to that the database will be created. Fore more info check this Code First Approach in Entity Framework

Model first

In Model First approach, we need to create entities, relationships and inheritance hierarchies directly on the design surface of EDMX and then it will generate database from it. For more info check this Model First Approach in Entity Framework

Topics Covered