Hello Friends, in this tutorial I am going to explain you about Entity Framework in.NET.

After completing this tutorial you will able to understand following topics:

  • Entity Framework in C#.
  • The concept of ORM (Object Relational Mapping).
  • Introduction to Code First Approach.
  • Introduction to Database First Approach.


Before taking a look at Entity Framework let’s understand what ORM (Object Relational Mapping) is?

What is Object-Relational Mapping?

ORM stands for Object Relational Mapping. The concept of ORM revolves around the business object of our application. ORM provides a mechanism using which we can map business objects of our application to the relational database so that the data can be retrieved, modified entirely through the object model. ORM provides a way to keep our database design separate from the business object. ORM also facilitates developers so that the developers doesn’t need to write the bunch of code to perform CRUD (Create, Read, Update, & Delete) operation.

Entity Framework

Let’s understand why we require ORM?

There are many ways to setup and create an application. But the problem occurs when the application grows. The things become more complicated when the new logic is added over time. With each new implementation, the database becomes bigger and complex. The code becomes complex to understand. With each and every change that we make to our application, existing functionality starts break also changing one will affect the other. This happens because our database and front-end are lightly coupled. ORM provides a way to get rid of this problem. The basic idea that the ORM technology provides is to abstract database access from the rest of the application by encapsulating the database access with its own layer and representing the data from the database within the business objects as opposed to working directly with fields from the database.

There are many ORM frameworks for .net

  •   DataObjects. Net
  •   NHibernate
  •   Open Access
  •   SubSonic

What is Entity Framework?

Entity Framework was introduced as a part of.NET Framework 3.5 SP1 and later. Entity Framework facilitates developers by eliminating most of the data access code. Entity Frameworks uses Linq queries to retrieve and manipulate data as strongly typed object. Entity Framework is an enhanced version of ADO.NET that provides the developer with an automated way to perform CRUD operation on the database.

Entity Framework

Scenarios where the Entity Framework should be used?

  1. When you have an existing database or you want to extend your database for another part of the application.
  2. When you want to consider your domain class first and then create a database from domain class of the application.
  3. You want to create your database schema on the visual designer and then create the database and classes.

Entity Framework follows two approaches:

1-  Code First Approach- As the name sounds code first rather than database first. With Code First Approach we can focus on creating classes as per domain requirement rather database design first.

Entity Framework

2-  Data Base First Approach- Database first approach focuses on the Database. It creates Database based on your domain class.

Entity Framework

So Entity Framework creates data access classes from our existing database so that we can use these classes to interact with the database instead of ADO.NET. Also, Entity Framework creates a database from our domain classes so that we can focus on domain driven design.

We will discuss more on both these approaches (Code First Approach and Database First Approach) in later sessions.

Hope this is a useful topic for you.

Thank You.

Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, bool given in C:\inetpub\vhosts\debugonweb.com\httpdocs\wp-includes\wp-db.php on line 3353