Create Entity framework file
Go to Visual Studio and create Windows Forms Application and click OK as shown in Fig 1.
Go to Solution Explorer, select the solution click with right mouse button and then click on Add. Click on New Item as in the following figure Fig:2.
Select Data from Visual C# ItemS in installed template. Select ADO .NET Entity Data Model, type the model name within parenthesis Model1.edmx, click on Add as shown in Fig:3,
Select Generate from database option click on next, then again next as in the following 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 entity connection settings in App.Config as, click on Finish as in the following figure 5,
Now choose database objects in Entity Data Model Wizard and select Table which we would like to display then click on Finish. Here's figure 6 showing the same,
Design the form as in the following 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
- {
- publicpartialclassForm1: Form
- {
- CMS1Entities objet = newCMS1Entities();
- public Form1()
- {
- InitializeComponent();
- }
-
- privatevoid Form1_Load(object sender, EventArgs e)
- {
- dataGridView1.DataSource = objet.EMP.ToList();
- }
-
- privatevoid btnSave_Click(object sender, EventArgs e)
- {
- objet.SaveChanges();
- MessageBox.Show("Data has saved/display successfully");
- }
- }
- }
Now run the application and check it.
Recap: Entity framework is an OR mapping tool. The methods of Entity framework is
SaveChanges ().