Click here to Skip to main content
13,288,121 members (54,107 online)
Click here to Skip to main content
Add your own
alternative version


370 bookmarked
Posted 9 Apr 2012

An Introduction to Entity Framework for Absolute Beginners

, 9 Apr 2012
Rate this:
Please Sign up or sign in to vote.
An introduction to Entity Framework for absolute beginners.


This article introduces Entity Framework to absolute beginners. The article is meant for developers who are primarily using ADO.NET to write their data access layers. Many experienced developers will find this article very basic but since the article is written from the perspective of beginners, I've tried to keep things simple.


ADO.NET is a very strong framework for data access. ADO.NET has been around since many years and there are a lot of systems running over ADO.NET. Developers who are totally oblivious to the concept of ORMs will probably be asking "What is Entity Framework? What are the benefits of using it and is it an alternative to ADO.NET?"

Well, to answer the first question about what is Entity Framework, Entity Framework is an Object Relational Mapper (ORM). It basically generates business objects and entities according to the database tables and provides the mechanism for:

  1. Performing basic CRUD (Create, Read, Update, Delete) operations.
  2. Easily managing "1 to 1", "1 to many", and "many to many" relationships.
  3. Ability to have inheritance relationships between entities.

and to answer the second question, the benefits are:

  1. We can have all data access logic written in higher level languages.
  2. The conceptual model can be represented in a better way by using relationships among entities.
  3. The underlying data store can be replaced without much overhead since all data access logic is present at a higher level.

and finally, the last question that whether it is an alternative to ADO.NET, the answer would be "yes and no". Yes because the developer will not be writing ADO.NET methods and classes for performing data operations and no because this model is actually written on top of ADO.NET, meaning under this framework, we are still using ADO.NET. So let us look at the architecture of Entity Framework (diagram taken from MSDN):

Entity framework article image

Using the code

Let's try to understand the ease of use that Entity Framework provides by performing simple CRUD operations. Once we look at the code and how effortlessly and efficiently we can do these operations, the benefits of Entity Framework will become quite obvious.

Creating the database

Let's have a simple database with one table. Let's create a simple table for Contacts and we will perform CRUD operations on this table.

Entity framework article image

Adding the Entity Model to the Website

Once we have the database ready, we can add the entity model to our website. We can do this by adding an ADO.NET Entity Data Model to the website.

Entity framework article image

Once we select to add this data model to our website, we will have to select the approach we want to take for our Model's contents.

Entity framework article image

What this selection means is that we can either choose to generate the entity model from an existing database schema or we can design the entity model here and then later hook it up to the database. Since we already have the database ready, we will use the first option. Once the Model is generated, the Entity for each table is generated. The generated entity for our contact table is:

Entity framework article image

Also, the classes for performing database operations are also created. We just need to know how to use these classes to perform database operations.

Entity framework article image

Insert operation

Let us create a simple page to perform an insert operation.

Entity framework article image

Now once the user chooses to insert the values into the database, the actual data operation can be performed by using the AddObject method of the Model class entity collection. The following code snippet show how to perform the insert.

Contact con = new Contact();
con.fname = TextBox1.Text;
con.lname = TextBox2.Text; = TextBox3.Text;

ContactsDb db = new ContactsDb();

This will insert the record into the table. You can notice the simplicity and efficiency of the code we wrote to perform the insertion.

Reading all the records

There are scenarios when we want to read all records. Let's say we are making a page that will display all the contact information in a single page.

Entity framework article image

We can retrieve the collection of Entities using the Model object to achieve this. The code snippet below will show how that can be done.

ContactsDb db = new ContactsDb();
Repeater1.DataSource = db.Contacts;

Selecting a specific record

If we want to select a specific record from the table, we can use the SingleOrDefault method on the Model's entities collection. Let's say we want the functionality of updating/deleting a record on a single page then we will first have to select the record based on the ID, then update/delete the selected record.

Entity framework article image

Selection of any particular record (Contact) based on ID can be done as:

int idToupdate = Convert.ToInt32(Request.QueryString["id"].ToString());
ContactsDb db = new ContactsDb();
Contact con = db.Contacts.SingleOrDefault(p => == idToupdate);

Once this code is executed, the Contact object will contain the required values.

Updating the record

If we want to update a record, then a simple update operation can be performed as:

int idToupdate = Convert.ToInt32(Request.QueryString["id"].ToString());
ContactsDb db = new ContactsDb();
Contact con = db.Contacts.SingleOrDefault(p => == idToupdate); = TextBox1.Text;

Once this code executes, the value of phone number will be updated by a new value which is retrieved from TextBox1.

Deleting a record

If we want to delete a particular record then we can perform a delete operation by using the DeleteObject function. The following code snippet demonstrates the same:

//delete this contact
int idToupdate = Convert.ToInt32(Request.QueryString["id"].ToString());
ContactsDb db = new ContactsDb();
Contact con = db.Contacts.SingleOrDefault(p => == idToupdate);


Now that we have the basic CRUD operations performed on the database using the Entity Framework.

A note on Relationships and Lazy Loading

To understand the Entity Framework we need to understand some things like naming conventions, relationships between tables, and relationships between entities. Also, the concept of lazy loading once fully understood will give the power to the developer to use the Entity Framework efficiently.

Note: Since this is an introductory article on Entity Framework we have not discussed these things. Perhaps we will discuss them in a separate article.

Points of interest

Entity Framework has been in use for some time now. But there are many developers who are still getting started with Entity Framework. This article was meant as an overview of the Entity Framework. This should not be treated as a complete tutorial. Also, the code written is very simple and there is a lot of scope for optimization but since the idea here is to understand Entity Framework, I tried to keep the code simple and readable rather and optimal.

Note: To run the solution, please change the database path of ConnectionString in the web.config file.


  • 09 April 2012: First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more:[^]

  • Microsoft MVP 2015

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Edo Tzumer27-Jan-13 4:36
memberEdo Tzumer27-Jan-13 4:36 
QuestionVs 2008 not support for this?? Pin
sarathkumarnallathambi24-Dec-12 2:28
membersarathkumarnallathambi24-Dec-12 2:28 
AnswerRe: Vs 2008 not support for this?? Pin
Rahul Rajat Singh24-Dec-12 19:26
memberRahul Rajat Singh24-Dec-12 19:26 
GeneralRe: Vs 2008 not support for this?? Pin
sarathkumarnallathambi25-Dec-12 18:30
membersarathkumarnallathambi25-Dec-12 18:30 
GeneralMy vote of 4 Pin
sarathkumarnallathambi24-Dec-12 1:08
membersarathkumarnallathambi24-Dec-12 1:08 
QuestionPls Reply Pin
Ankit Kumar Rajput7-Dec-12 2:13
memberAnkit Kumar Rajput7-Dec-12 2:13 
aaltintop23-Nov-12 2:09
memberaaltintop23-Nov-12 2:09 
GeneralMy vote of 5 Pin
Member 292641819-Nov-12 2:33
memberMember 292641819-Nov-12 2:33 
GeneralMy vote of 4 Pin
Member 403116612-Nov-12 1:14
memberMember 403116612-Nov-12 1:14 
GeneralMy vote of 5 Pin
Bhupendra Chauhan1-Nov-12 20:26
memberBhupendra Chauhan1-Nov-12 20:26 
GeneralNice Article Rahul Pin
Shivanand Arur15-Oct-12 22:19
memberShivanand Arur15-Oct-12 22:19 
GeneralThanks... Pin
Kinzever8-Oct-12 20:48
memberKinzever8-Oct-12 20:48 
GeneralMy vote of 5 Pin
Vaso0078-Oct-12 3:39
memberVaso0078-Oct-12 3:39 
GeneralVote 3 Pin
Vaso0078-Oct-12 3:37
memberVaso0078-Oct-12 3:37 
Hi Rahul, I am referencing to a few Vote 3s here. It seems that "Absolute Beginners" has got many meanings for community. Maybe, you could mention in the beginning that this article is rather for c#/sql little experienced and at the same time also Absolute Beginners in Entity Framework.

This way somebody who doestn't understand c# or how to create a table or how to connect to the database so the model is generated won't be confused and rating 3.

Simply, pre-required knowledge/skills in the beginning of article might suggest straigh away if the person is going to understand or not Smile | :)

I think this article deservers 5 Smile | :)

Thank you.

Questionrating Pin
krupeshshah4-Oct-12 21:11
memberkrupeshshah4-Oct-12 21:11 
GeneralMy vote of 3 Pin
Bhawesh jha28-Sep-12 0:32
memberBhawesh jha28-Sep-12 0:32 
AnswerArticle of the Day on Microsoft's site Pin
Rahul Rajat Singh25-Sep-12 20:01
memberRahul Rajat Singh25-Sep-12 20:01 
GeneralMy vote of 5 Pin
AnantD24-Sep-12 0:56
memberAnantD24-Sep-12 0:56 
AnswerRe: My vote of 5 Pin
Rahul Rajat Singh24-Sep-12 2:02
memberRahul Rajat Singh24-Sep-12 2:02 
GeneralMy vote of 5 Pin
Velkumar Kannan21-Sep-12 21:55
memberVelkumar Kannan21-Sep-12 21:55 
AnswerRe: My vote of 5 Pin
Rahul Rajat Singh21-Sep-12 23:42
memberRahul Rajat Singh21-Sep-12 23:42 
QuestionSmaller typos Pin
Michael Pauli19-Sep-12 3:08
memberMichael Pauli19-Sep-12 3:08 
QuestionDatabase not working Pin
n_sasi10-Sep-12 2:47
membern_sasi10-Sep-12 2:47 
AnswerRe: Database not working Pin
Rahul Rajat Singh10-Sep-12 19:04
memberRahul Rajat Singh10-Sep-12 19:04 
Suggestionvisit Pin
Ravi Karia12-Aug-12 4:20
memberRavi Karia12-Aug-12 4:20 
NewsVery Nice! Pin
Member 47137854-Aug-12 1:17
memberMember 47137854-Aug-12 1:17 
Questionnice article Pin
Ravi Karia2-Aug-12 7:38
memberRavi Karia2-Aug-12 7:38 
GeneralMy vote of 5 Pin
amit singhniet31-Jul-12 1:01
memberamit singhniet31-Jul-12 1:01 
GeneralMy vote of 5 Pin
Sk. Tajbir14-Jun-12 3:59
memberSk. Tajbir14-Jun-12 3:59 
QuestionMy vote of 3 Pin
Clifford Nelson17-May-12 8:45
memberClifford Nelson17-May-12 8:45 
GeneralMy vote of 3 Pin
SChristmas24-Apr-12 8:27
memberSChristmas24-Apr-12 8:27 
GeneralMy vote of 4 Pin
Som P23-Apr-12 10:36
memberSom P23-Apr-12 10:36 
GeneralMy vote of 5 Pin
Anurag Gandhi21-Apr-12 10:25
memberAnurag Gandhi21-Apr-12 10:25 
General10x Pin
raananv18-Apr-12 11:06
memberraananv18-Apr-12 11:06 
QuestionWould this be something like an automated Business Object Layer? Pin
jaimepremy17-Apr-12 15:02
memberjaimepremy17-Apr-12 15:02 
GeneralMy vote of 5 Pin
qiyueyue17-Apr-12 4:01
memberqiyueyue17-Apr-12 4:01 
QuestionThanks Pin
philou105216-Apr-12 0:55
memberphilou105216-Apr-12 0:55 
GeneralMy vote of 5 Pin
Glen_Beamon10-Apr-12 10:01
memberGlen_Beamon10-Apr-12 10:01 
QuestionBest EntityFramework ebook Pin
amitgajjar9-Apr-12 21:31
memberamitgajjar9-Apr-12 21:31 
AnswerRe: Best EntityFramework ebook Pin
Rahul Rajat Singh9-Apr-12 21:34
memberRahul Rajat Singh9-Apr-12 21:34 
GeneralRe: Best EntityFramework ebook Pin
amitgajjar9-Apr-12 21:36
memberamitgajjar9-Apr-12 21:36 
GeneralRe: Best EntityFramework ebook Pin
Rahul Rajat Singh1-May-12 2:31
memberRahul Rajat Singh1-May-12 2:31 
GeneralRe: Best EntityFramework ebook Pin
amitgajjar2-May-12 18:55
memberamitgajjar2-May-12 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 9 Apr 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid