In this article I will explain with an example, how to delete multiple records using Entity Framework with C# and VB.Net in Windows Forms (WinForms) Application.
 
 

Database

I have made use of the following table Customers with the schema as follow.
Delete multiple records using Entity Framework in C# and VB.Net
 
I have already inserted few records in the table.
Delete multiple records using Entity Framework in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
            Download SQL file
 
 

Form Design

The following Form consists of:
Button – For deleting multiple records.
Delete multiple records using Entity Framework in C# and VB.Net
 
 

Configuring and connecting Entity Framework to database

First, you need to configure and connect Entity Framework to database.
Note: For more details on how to configure and connect Entity Framework to database, please refer my article Connect and configure Entity Framework step by step in Windows Forms Application.
 
 

Deleting multiple records from Database using Entity Framework

The following event handler is executed when the Delete Button is clicked.
Inside the event handler, CustomerIds to be deleted are fetched from the TextBox and converts them into an Integer Array.
Then based on the Array, Customers are referenced and passed to the RemoveRange method of Entity Framework and the SaveChanges method of Entity Framework is called which updates the changes to the database.
Finally, numbers of record deleted is displayed in MessageBox.
C#
private void OnDelete(object sender, EventArgs e)
{
    using (AjaxSamplesEntities entities = new AjaxSamplesEntities())
    {
        int rowsAffected = 0;
        int[] customerIds Array.ConvertAll(txtIds.Text.Split(','), Int32.Parse);
 
        // Referencing Customers.
        List<Customer> deletedCustomers entities.Customers.Where(customer => customerIds.Contains(customer.CustomerId)).ToList();
        if (deletedCustomers != null)
        {
            // Deleting Customers.
            rowsAffected entities.Customers.RemoveRange(deletedCustomers).Count();
            entities.SaveChanges();
        }
 
        MessageBox.Show(string.Format("{0} record/s are deleted.", rowsAffected));
    }
 
    txtIds.Text = string.Empty;
}
 
VB.Net
Private Sub OnDelete(sender As Object, e As EventArgs) Handles btnDelete.Click
    Using entities As AjaxSamplesEntities = New AjaxSamplesEntities()
        Dim rowsAffected As Integer = 0
        Dim customerIds As Integer() = Array.ConvertAll(txtIds.Text.Split(","c), Function(s) Int32.Parse(s))
 
        ' Referencing Customers.
        Dim deletedCustomers As List(Of Customer) = entities.Customers.Where(Function(customer) customerIds.Contains(customer.CustomerId)).ToList()
        If deletedCustomers IsNot Nothing Then
            ' Deleting Customers.
            rowsAffected entities.Customers.RemoveRange(deletedCustomers).Count()
            entities.SaveChanges()
        End If
 
        MessageBox.Show(String.Format("{0} record/s are deleted.", rowsAffected))
    End Using
 
    txtIds.Text = String.Empty
End Sub
 
 

Screenshot

Delete multiple records using Entity Framework in C# and VB.Net
 

Records After Deleting

Delete multiple records using Entity Framework in C# and VB.Net
 
 

Downloads