How to Delete Data Using Entity Framework in WPF

Introduction

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

Create a WPF Application as in Figure 1.


Figure 1: Create WPF application

Add an ADO.NET entity data model to the project as in Figures 2 and 3.

 

Figure 2: Add Entity Framework

 

Figure 3: Wizard

 

Figure 4: Configure connection

 

Figure 5: Choose version

 

Figure 6: Select database objects

MainWindow.xaml

  1. <Window x:Class="DeleteData_WPF_EF.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="525"  
  6.         Height="350"  
  7.         Loaded="Window_Loaded">  
  8.     <Grid>  
  9.         <DataGrid x:Name="dgEmployee"  
  10.                   Width="267"  
  11.                   Margin="118,47,0,0"  
  12.                   HorizontalAlignment="Left"  
  13.                   VerticalAlignment="Top"  
  14.                   AutoGenerateColumns="False"  
  15.                   CanUserAddRows="False"  
  16.                   CanUserDeleteRows="True"  
  17.                   ColumnWidth="*">  
  18.             <DataGrid.Columns>  
  19.                 <DataGridTextColumn x:Name="dgrEmpId"  
  20.                                     Binding="{Binding EmpId}"  
  21.                                     Header="EmpId"  
  22.                                     IsReadOnly="True" />  
  23.                 <DataGridTextColumn x:Name="dgrFirstName"  
  24.                                     Binding="{Binding FirstName}"  
  25.                                     Header="FirstName"  
  26.                                     IsReadOnly="True" />  
  27.                 <DataGridTextColumn x:Name="dgrLastName"  
  28.                                     Binding="{Binding LastName}"  
  29.                                     Header="LastName"  
  30.                                     IsReadOnly="True" />  
  31.                 <DataGridTemplateColumn>  
  32.                     <DataGridTemplateColumn.CellTemplate>  
  33.                         <DataTemplate>  
  34.                             <Button x:Name="btnDelete"  
  35.                                     Click="btnDelete_Click"  
  36.                                     Content="Delete" />  
  37.                         </DataTemplate>  
  38.                     </DataGridTemplateColumn.CellTemplate>  
  39.                 </DataGridTemplateColumn>  
  40.             </DataGrid.Columns>  
  41.         </DataGrid>  
  42.     </Grid>  
  43. </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. namespace DeleteData_WPF_EF  
  16. {  
  17.     /// <summary>  
  18.     /// Interaction logic for MainWindow.xaml  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         EmployeeDBEntities objEntities = new EmployeeDBEntities();  
  23.         public MainWindow()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  28.         {  
  29.             dgEmployee.ItemsSource = objEntities.Employees.ToList();  
  30.         }  
  31.   
  32.         private void btnDelete_Click(object sender, RoutedEventArgs e)  
  33.         {  
  34.             int empId = (dgEmployee.SelectedItem as Employee).EmpId;  
  35.             Employee employee = (from r in objEntities.Employees where r.EmpId == empId select r).SingleOrDefault();  
  36.             objEntities.Employees.Remove(employee);  
  37.             objEntities.SaveChanges();  
  38.             dgEmployee.ItemsSource = objEntities.Employees.ToList();  
  39.         }  
  40.     }  
  41. }

The output of the application is as in Figures 7 and 8.

 

Figure 7: Output of the application before delete

 

Figure 8: Output of the application after delete

Summary

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



MVC Corporation
MVC Corporation is consulting and IT services based company.