Create Entity framework file
Go to Visual Studio and create windows form application and click on OK as in the following figure 1:
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:
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:
Figure 3
Select Generate from database option click on next as in the following figure 4:
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.
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.
Figure 6
Design the form as in the following figure 7.
Figure 7
Write the following code,
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsEntityFramework
- {
- public partial class Form1: Form
- {
- CMS1Entities objet = new CMS1Entities();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {}
- private void btnInsert_Click(object sender, EventArgs e)
- {
- Employee obje = new Employee()
- Obje.eid = Convert.ToInt32(txtEid.Text);
- Obje.ename = txtEName.Text;
- Obje.edesignation = txtEdesignation.Text;
- Obje.esalary = Convert.ToDouble(txtsalary.Text);
- Object.Employee.Add(obje);
- Object.SaveChanges();
- MessageBox.Show("Data is Inserted to Database ");
- }
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- var rec(From Emp in object.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)
- .First();
- rec.EName = txtEName.Text;
- rec.EDesignation = txtDesignation.Text;
- rec.ESalary = Convert.ToInt32(txtESalary.Text);
- object.SaveChanges();
- MessageBox.Show("Record is Updated");
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- var rec(From Emp in object.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)
- .First();
- object.Employee.Remove(obje);
- object.SaveChanges();
- MessageBox.Show("Record is Deleted");
- }
- private void btnFind_Click(object sender, EventArgs e)
- {
- var rec(From Emp in objdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp)
- .First();
- txtEName.Text = rec.EName;
- txtDesignation.Text = rec.EDesignation;
- txtESalary.Text = rec.ESalary.ToString();
- }
- }
- }
Now run the application and check it.
Recap: Entity framework is an OR mapping tool. The methods of Entity framework is
SaveChanges ();