Using Entity Framework in Asp.Net MVC 4 with Example

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

 

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

 

Now let’s have a look at 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 the "Entity Framework" in three different ways.

 

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

Database first

In the Database First approach, we use an existing database to create a 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. For more info, check this Code First Approach in Entity Framework.

Model first

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