Introduction

In my last article we discussed about the edmx file. I hope now you are little bit familiar with entity framework and we can start the basic functionality like insert, update, delete and select.

In this article I am going to explain how we can insert data into database using Entity Framework.

As we already added edmx in our application.

Now create the form from where you will take input from the user

For Example

Now create the form from where you will take input from the user


		
insertion in ef

		Figure 1
		

Now on button click add the following code:-




		
//object of entity class which holds all methods
        techaltum_efEntities te = new techaltum_efEntities();

        //object of student class to take input
        student stu = new student();

        stu.stu_name = txt_name.Text;
        stu.age = Convert.ToInt32(txt_age.Text);
        stu.course = DropDownList1.SelectedItem.Text;

        //add entity to the add method
        te.students.Add(stu);

        //insert it into table
       int res= te.SaveChanges();

       if (res > 0)
       {

           Response.Write("Data Inserted Successfully");

       }
       else
       {

           Response.Write("Try Again!!!");
       
       }

 	

Now execute this code and enter data and click on insert button


		
how to insert data using ef

		Figure 2
		

Now go to database and check your table

	
		
insertion using ef

		Figure 3