Work With Entity Framework To Perform CRUD Operation

In this article I am explaining how to work with Entity framework to perform CRUD operation.

Create Entity framework file

Go to Visual Studio and create windows form application and click on OK as in the following figure 1:

windows form application
                                                            Figure 1

Go to solution explorer, select the solution click with right mouse button and then click Add. Click on New Item as in the following figure 2:

Add new item
                                                                        Figure 2

Select Data from Visual C# item in installed template. Select ADO .NET Entity Data Model, type the model name within parenthesis Model1.edmx, click on Add as in the following figure 3:

parenthesis Model1
                                                                     Figure 3

Select Generate from database option click on next as in the following figure 4:

parenthesis
                                                                        Figure 4

Click on New Connection, type the server name, select the radio button and use the windows authentication (also we can use SQL Server authentication). Select the database name within parenthesis and check the Save Connection setting in App.Configure and click on Finish as shown in Figure 5.

Connection
                                                                              Figure 5

Now choose database objects in Entity Data Model Wizard. And select Table which we would like to display then click on Finish. As shown in Figure 6.

check the Save Connection
                                                                  Figure 6

Design the form as in the following figure 7.

click on Finish
                                          Figure 7

Write the following code,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace WindowsEntityFramework  
  11. {  
  12.     public partial class Form1: Form  
  13.     {  
  14.         CMS1Entities objet = new CMS1Entities();  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void Form1_Load(object sender, EventArgs e)  
  20.         {}  
  21.         private void btnInsert_Click(object sender, EventArgs e)  
  22.         {  
  23.             Employee obje = new Employee()  
  24.             Obje.eid = Convert.ToInt32(txtEid.Text);  
  25.             Obje.ename = txtEName.Text;  
  26.             Obje.edesignation = txtEdesignation.Text;  
  27.             Obje.esalary = Convert.ToDouble(txtsalary.Text);  
  28.             Object.Employee.Add(obje);  
  29.             Object.SaveChanges();  
  30.             MessageBox.Show("Data is Inserted to Database ");  
  31.         }  
  32.         private void btnUpdate_Click(object sender, EventArgs e)  
  33.         {  
  34.             var rec(From Emp in object.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)  
  35.                 .First();  
  36.             rec.EName = txtEName.Text;  
  37.             rec.EDesignation = txtDesignation.Text;  
  38.             rec.ESalary = Convert.ToInt32(txtESalary.Text);  
  39.             object.SaveChanges();  
  40.             MessageBox.Show("Record is Updated");  
  41.         }  
  42.         private void btnDelete_Click(object sender, EventArgs e)  
  43.         {  
  44.             var rec(From Emp in object.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)  
  45.                 .First();  
  46.             object.Employee.Remove(obje);  
  47.             object.SaveChanges();  
  48.             MessageBox.Show("Record is Deleted");  
  49.         }  
  50.         private void btnFind_Click(object sender, EventArgs e)  
  51.         {  
  52.             var rec(From Emp in objdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)  
  53.                 .First();  
  54.             txtEName.Text = rec.EName;  
  55.             txtDesignation.Text = rec.EDesignation;  
  56.             txtESalary.Text = rec.ESalary.ToString();  
  57.         }  
  58.     }  
  59. }  
Now run the application and check it.

Recap: Entity framework is an OR mapping tool. The methods of Entity framework is SaveChanges ();