Share Blog

Thursday, May 29, 2014

Entity framework using Ado.net Insert,Update,Delete,Show,Search

What is Entity Framework?

Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database and working with the results in addition to DataReader and DataSet.


What are the benefits of using EF?

The main and the only benefit of EF is it auto-generates code for the Model (middle layer), Data Access Layer, and mapping code, thus reducing a lot of development time.

What is the importance of EDMX file in Entity Framework?

EDMX (Entity Data Model XML) is an XML file which contains all the mapping details of how your objects map with SQL tables. The EDMX file is further divided into three sections: CSDL, SSDL, and MSL.

·         1.CSDL (Conceptual Schema definition language) is the conceptual abstraction which is exposed to the application.

       2. SSDL (Storage Schema Definition Language) defines the mapping with your RDBMS data structure.

      3.   MSL (Mapping Schema Language) connects the CSDL and SSDL.


CSDL, SSDL and MSL are actually XML files.

Adding Entity Data Model:



In your project in solution explorer, right click > Add New Item > ADO.NET Entity Data Model
Give Name “Model.edmx” > Add
If it asks to add in APP_code folder, click ok
Select “Generate From Database” > Next
Select your database and Set “Save Entity connection string in Web.config as” option true giving name adonetpracticesEntities.
Select  emp1 tables
Give Model Namespace and click on Finish.
---After Desing Web Page-----



---Default.aspx Code-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using adonetpracticesModel;

public partial class _Default : System.Web.UI.Page
{
    adonetpracticesEntities entity = new adonetpracticesEntities();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      //Create new Emp object
        emp1 ob = new emp1();
        ob.id = Convert.ToInt32(TextBox1.Text);
        ob.name = TextBox2.Text;
        ob.address = TextBox3.Text;
          //Add to memory
        entity.AddToemp1(ob);
       //Save to database
        entity.SaveChanges();
        Response.Write("<script>alert('Data Save Successfully')</script>");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int m = Convert.ToInt32(TextBox1.Text);
        var v = from i in entity.emp1 orderby  i.id select i;
        foreach(emp1  item in v)
        {
            if (item.id == m)
            {
                item.name = TextBox2.Text;
                item.address = TextBox3.Text;
            }
        }
        entity.SaveChanges();
        Response.Write("<script>alert('Data Update Successfully')</script>");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
   
    protected void Button3_Click(object sender, EventArgs e)
    {
        int m = Convert.ToInt32(TextBox1.Text);
        var v = from i in entity.emp1 orderby i.id select i;
        foreach(emp1 item in v)
        {
            if (item.id == m)
            {
                entity.DeleteObject(item);
            }
            else
            {
                Response.Write("<script>alert('Data Not Found')</script>");
            }


        }
        entity.SaveChanges();
        Response.Write("<script>alert('Data Deleted Successfully')</script>");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        var v = from i in entity.emp1 select i;
        GridView1.DataSource = v;
        GridView1.DataBind();

    }
   protected void Button5_Click(object sender, EventArgs e)
    {
        int m = Convert.ToInt32(TextBox1.Text);
        var v= from i in entity.emp1  where  i.id==m select i;
      
                GridView1.DataSource = v;
                GridView1.DataBind();
           
       
    }

}



No comments:

Post a Comment