In this article I will explain a simple step by step tutorial with an example, how to connect and configure Entity Framework in Windows Forms (WinForms) application using C# and VB.Net.
This article will illustrate, how to configure Entity Framework and connect it to SQL Server database and then later use it to populate DataGridView control in Windows Forms (WinForms) application using C# and VB.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Configuring and connecting Entity Framework to database in Windows Forms Application
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database in Windows Forms application.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
Then the Entity Data Model Wizard will open up where you need to select EF Designer database option.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
Now the wizard will ask you to connect and configure the Connection String to the database.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
You will need to select the
1.     SQL Server Instance.
2.     Database.
And then click Test Connection to make sure all settings are correct.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
Once the Connection String is generated, click Next button to move to the next step.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
Next you will need to choose the Entity Framework version to be used for connection.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
Now you will need to choose the Tables, you need to connect and work with Entity Framework. Here Customers Table needs to be selected.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
 
Form Design
The below Form consists of a DataGridView control.
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
 
Populating DataGridView using Entity Framework
Inside the Form Load event, the records from the Customers Table are fetched using Entity Framework and are assigned to the DataSource property of the DataGridView control.
C#
private void Form1_Load(object sender, EventArgs e)
{
    NorthwindEntities entities = new NorthwindEntities();
    var customers = from p in entities.Customers
                    select new
                    {
                        CustomerId = p.CustomerID,
                        ContactName = p.ContactName,
                        Country = p.Country
                    };
    dataGridView1.DataSource = customers.ToList();
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim entities As NorthwindEntities = New NorthwindEntities()
    Dim customers = From p In entities.Customers _
                    Select New With {.CustomerId = p.CustomerID, _
                                     .ContactName = p.ContactName, _
                                     .Country = p.Country}
    dataGridView1.DataSource = customers.ToList()
End Sub
 
 
Screenshot
Connect and configure Entity Framework step by step in Windows Forms Application using C# and VB.Net
 
 
Downloads