In this article I will explain with an example, how to delete all records using Entity Framework in ASP.Net.
 
 

Database

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

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 Configure Entity Framework Step By Step in ASP.Net.
 
 

HTML Markup

The HTML Markup consists of following controls:
Button – For deleting all records.
The Button has been assigned with an OnClick event handler.
<form id="form1" runat="server">
    <asp:Button runat="server" OnClick="OnDeleteAll" Text="Delete All" />
</form>
 
 

Deleting all records from Database using Entity Framework

The following event handler is executed when the Delete All Button is clicked.
Inside the event handler, Customer Collection is 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, based on whether record is deleted, an appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnDeleteAll(object sender, EventArgs e)
{
    using (AjaxSamplesEntities entities = new AjaxSamplesEntities())
    {
        // Deleting all records from Database Table.
        entities.Customers.RemoveRange(entities.Customers);
        entities.SaveChanges();
        ClientScript.RegisterStartupScript(this.GetType(), "alert""alert('Customer records are deleted.');"true); 
    }
}
 
VB.Net
Private Sub OnDeleteAll(sender As Object, e As EventArgs)
    Using entities As AjaxSamplesEntities = New AjaxSamplesEntities()
        ' Deleting all records from Database Table.
        entities.Customers.RemoveRange(entities.Customers)
        entities.SaveChanges()
        ClientScript.RegisterStartupScript(Me.GetType(), "alert""alert('Customer record are deleted.');"True)
    End Using
End Sub
 
 

Screenshot

Delete all records using Entity Framework in ASP.Net
 
 

Downloads