Select Data Using Entity Framework in WPF

Introduction

This article shows how to select data using Entity Framework in WPF applications.

Create a WPF application as in Figure 1.


Figure 1: Create WPF application

Add Entity Framework to the project as in Figures 2, 3, 4, 5, 6 and 7.

 
Figure 2: Add Entity Framework

 
Figure 3: Entity Data Model wizard

 
Figure 4: Choose connection

 
Figure 5: Choose Entity Framework version

 
Figure 6: Choose database object

 
Figure 7: Model designer

MainWindow.xaml
  1. <Window x:Class="SelectData_WPF_EFApp.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow"  
  5.         Width="735.448"  
  6.         Height="350"  
  7.         Loaded="Window_Loaded">  
  8.     <DataGrid x:Name="dgEmployee"  
  9.               Width="221"  
  10.               Margin="242,47,0,0"  
  11.               HorizontalAlignment="Left"  
  12.               VerticalAlignment="Top"  
  13.               AutoGenerateColumns="False"  
  14.               CanUserAddRows="False"  
  15.               ColumnWidth="*">  
  16.         <DataGrid.Columns>  
  17.             <DataGridTextColumn x:Name="dgrEmpId"  
  18.                                 Binding="{Binding EmpId}"  
  19.                                 Header="EmpId"  
  20.                                 IsReadOnly="True" />  
  21.             <DataGridTextColumn x:Name="dgrFirstName"  
  22.                                 Binding="{Binding FirstName}"  
  23.                                 Header="FirstName"  
  24.                                 IsReadOnly="True" />  
  25.             <DataGridTextColumn x:Name="dgrLastName"  
  26.                                 Binding="{Binding LastName}"  
  27.                                 Header="LastName"  
  28.                                 IsReadOnly="True" />  
  29.         </DataGrid.Columns>  
  30.     </DataGrid>  
  31. </Window>  

MainWindow.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15.   
  16. namespace SelectData_WPF_EFApp  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for MainWindow.xaml  
  20.     /// </summary>  
  21.     public partial class MainWindow : Window  
  22.     {  
  23.         public MainWindow()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  29.         {  
  30.             dgEmployee.ItemsSource = objEmployeeEntities.Employees.ToList();  
  31.         }  
  32.   
  33.         EmployeeDBEntities objEmployeeEntities = new EmployeeDBEntities();  
  34.     }  
  35. }  

The output of the application is as in Figure 8.

Output of the application
Figure 8:
Output of the application

Summary

In this article we saw how to select data using Entity Framework in WPF applications.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.