Entity Framework Demo

This article introduce entity framework. In this article we learn Entity frame work. This article for those who is beginner in entity framework who is already done work with ado.net. In this article we discuss about Entity frame work.

  1. Why we use entity framework
    1. If you don’t want to write Ado.Net code for connecting to the database table.
    2. Entity frame work code common for all database like sql,oracle and any other database because its translated at runtime by the particular provider of database.
    3. Entity frame work is good if you want to move your database from sql to oracle and any other database and then you need to changes all query which you have written in sql or other database. If you use entity frame work in your application don’t need to worry about database migration because in entity framework queries written in linq and this query translated at runtime.
    4. Using entity frame work we can combine tables from different database and also from different server.

  2. What is entity framework?

    It is a type of Data access layer (DAL).there are different Data access layer in .Net technology.
    1. Ado.Net
    2. Entity framework
    3. NHibernate

    Entity frame work is an ORM (object relation mapping) frame work that enable the developer to insert, update, delete and retrieve record from database without writing much more code to access database.

    ORM- This frame work allows speeding up your development process and increasing maintainability of code. This frame work allows interacting with database using object.

    Entity framework

  3. CRUD Operation using Entity Framework

    Now I am creating a application in which we learn how to use entity framework in asp.net and how to use it to perform CURD (create update retrieve delete) operation in database.

    Creating Database

    Let’s create a table for employee (Name of table tblemployee) in database and we will perform CRUD operation on this table.

    DataBase Table

    Adding Ado.Net entity model in website

    Entity Data Model

    Once we select entity data model to add in our website then we will have to need choose model contents.

    Entity Data Model Wizard

    Here we have two options one is generate from existing database and second one is empty model.

    1. Generate from database->this option means create entity model from existing database schema. If we have already database ready then we use this option.
    2. Empty model->using this we can design model and hook up model later to the database.

    Here we use Generate from database after adding this generated entity for tblEmployee is

    Entity Table

    And also class form performing database operation is already created. Now we just need to know how to use it.

    Insert and Display(Retrieve)

    Insert Data Using Entity Framework

    Insert a record

    Using this screen we can add new employee in our database table using entity framework and display all employee record. We need to some line of code for adding record in database.Add this code on button click event.

    Inserting Record

    1. //Add new record in table
    2. TestEntities t = new TestEntities();
    3. tblEmployee employee = new tblEmployee();
    4. employee.name = txtname.Text.Trim();
    5. employee.age =Convert.ToInt16(txtage.Text.Trim());
    6. t.AddTotblEmployees(employee);
    7. t.SaveChanges();
    8. Bindgridview();
    Using AddTotblEmployees method we can add new record in database actual operation perform by AddTotblEmployees this method. Here we can also see how to retrieve data from database table and display in girdview.

    Retrieve Record

    In this method we have to write code for binding gridview with employee data.

    Retieving Record

    1. private void Bindgridview()
    2. {
    3. TestEntities test = new TestEntities();
    4. gdvEmployeerecord.DataSource = test.tblEmployees;
    5. gdvEmployeerecord.DataBind();
    6. }
    Update and Delete

    Upload and Delete table

    Using this screen we can update existing particular employee and delete particular employee.
    At first we need to retrieve particular employee record from database so that we need to put this code for retrieving data from database. Write this code on Next button click. This code populates employee name and age in textbox according to employee id.

    Inserting Record

    Updating a Record

    After that we can perform changes .If we want to update record of particular id we need to put this code in update button click.

    Updating Record

    Deleting a Record

    If we want to delete particular record then we need to put this code in delete button click.

    Delete Record

Thanks for reading my article. Please Download Attached file for complete demo.