ADO.Net Entity Framework Demo

This article introduces the Entity Framework. In this article, we learn the Entity Framework. This article is for Entity Framework beginners that have already worked with ADO.Net. This article explains the Entity Framework.
  • Why we use Entity Framework
  • What Entity Framework is
  • CRUD (create, read, update and delete) operations using Entity Framework.

1. Why we use Entity Framework

  • If you don't want to write ADO.Net code for connecting to the database table.
  • Entity Framework code is common for all databases, like SQL, Oracle, and any other database because it's translated at runtime by the specific provider of the database.
  • Entity Framework is good if you want to move your database from SQL to Oracle and any other database and then you need to change all the queries that you have written in SQL or another database. If you use Entity Framework in your application then you don't need to worry about database migration because in Entity Framework queries are written in LINQ and translated at runtime.
  • Using Entity Framework we can combine tables from different databases and also from a different server.

2. What Entity Framework is

The Entity Framework is a type of Data access layer (DAL). There are many Data Access Layers in the .Net technology.
  1. ADO.Net
  2. Entity Framework
  3. NHibernate
Entity Framework is an Object Relation Mapping (ORM) framework that enables the developer to insert, update, delete and retrieve records from a database without writing much code to access the database.


This framework allows speeding up your development process and increasing the maintainability of the code. This framework allows interacting with the database using an object.

3. CRUD Operations using Entity Framework

Now I am creating an application in which we learn how to use Entity Framework in ASP.NET and how to use it to do CRUD (create, read, update and delete) operations in a database.
Creating Database
Let's create a table for employees (the name of the table is tblemployee) in the database and we will do CRUD operations on this table.

Adding ADO.Net entity model into the website

Once we select the entity data model to add to our website we will need to choose the model contents.
Here we have two options, one is to generate from an existing database and the second is an empty model.
  1. Generate from the database: this option means to create an entity model from an existing database schema. If we already have a database ready then we use this option.
  2. Empty model: using this we can design the model and hook up the model layer to the database.
Here we use Generate from the database after adding this generated entity for tblEmployee:
And also the class form for performing database operations is already created. Now we just need to learn how to use it.

Insert and Display (Retrieve)


Insert a record

Using this screen we can add a new employee to our database table using Entity Framework and display all the employee records. We need to add a line of code for adding records to the database. Add this code on button click event.
  1. //Add a new record in the table  
  2.  TestEntities t = new TestEntities();  
  3.  tblEmployee employee = new tblEmployee();  
  4. = txtname.Text.Trim();  
  5.  employee.age =Convert.ToInt16(txtage.Text.Trim());  
  6.  t.AddTotblEmployees(employee);  
  7.  t.SaveChanges();  
  8.  Bindgridview(); 
Using the AddTotblEmployees method we can add a new record to the database actual operation performed by AddTotblEmployees this method. Here we can also see how to retrieve data from the database table and display it in a girdview.

Retrieve Record

In this method, we need to write code for binding the GridView with employee data.
  1. private void Bindgridview()  
  2.     {  
  3.         TestEntities test = new TestEntities();  
  4.         gdvEmployeerecord.DataSource = test.tblEmployees;  
  5.         gdvEmployeerecord.DataBind();  
  6.     } 

Update and Delete

Using this screen we can update an existing specific employee and delete a specific employee.
At first, we need to retrieve a specific employee record from the database so that we need to use this code for retrieving data from the database. Write this code on the Next button click. This code populates the employee name and age in a TextBox depending on the employee id.
  1. Int16 empid = Convert.ToInt16(txtEmpId.Text.Trim());  
  2. TestEntities t = new TestEntities();  
  3. tblEmployee emp = t.tblEmployees.FirstOrDefault(p => == empid);  
  4. txtname.Text =;  
  5. txtage.Text = emp.age.ToString(); 

Updating a Record

Then we can do changes. If we want to update a record of a specific id then we need to put this code in an update button click.
  1.             //save the updated record  
  2.             Int16 empid = Convert.ToInt16(txtEmpId.Text.Trim());  
  3.             TestEntities t = new TestEntities();  
  4.             tblEmployee emp = t.tblEmployees.FirstOrDefault(p => == empid);  
  5.    = txtname.Text.Trim();  
  6.             emp.age = Convert.ToInt16(txtage.Text.Trim());  
  7.             t.SaveChanges();  
  8.             lblErrormesg.Visible = true;  
  9.             lblErrormesg.BackColor = Color.Blue;  
  10.             lblErrormesg.Text = "Record Updated."

Deleting a Record

If we want to delete a specific record then we need to use this code in the delete button click.
  1.             //Delete record from the database.  
  2.             Int16 empid = Convert.ToInt16(txtEmpId.Text.Trim());  
  3.             TestEntities t = new TestEntities();  
  4.             tblEmployee emp = t.tblEmployees.FirstOrDefault(p => == empid);  
  5.             t.DeleteObject(emp);  
  6.             t.SaveChanges();  
  7.             lblErrormesg.Visible = true;  
  8.             lblErrormesg.BackColor = Color.Red;  
  9.             lblErrormesg.Text = "Record Deleted."
Thanks for reading my article. Please download the attached file for the complete demo.