Deleting Records in Entity Framework

Learn how to delete records from the database using Entity Framework. Deleting an entity is done using the Remove or RemoveRange method of the DbSet. Alternatively, you can also set the entity state as Deleted. We can delete records either in connected or disconnected Scenarios. We will also look at how to remove multiple records from the database using the RemoveRange method.

Source Code:
The source code of this project available in GitHub.

You can refer to our following tutorial on

Delete a Record

How you delete the entity depends on whether you are deleting the record in Connected or Disconnected scenario. In Connected Scenario, you can use the Remove or RemoveRange method to mark the record as Deleted. In Disconnected Scenario, you can attach it to the context and set its state as Deleted. Calling SaveChanges will send the delete query to the database.

Connected Scenario

Deleting in a connected scenario is very straight forward. Query the Department entity from the database. Call Remove method and pass the Department object to delete. The Change tracking in Entity Framework marks the entity as Deleted. Finally, SaveChanges will remove the Department from the database using the Delete Query

SQL Query

Disconnected Scenario

The following is the disconnected scenario example. First, we retrieve the Department entity and close the context. Later we open a new context and use the db.Entry method to set its state as Deleted. If the model is not in the context, then the Entry method adds it to the context and sets its state as Deleted

Attach Method

You can also use the Attach and remove method as shown below. Note that Attach will add the entity to the context in Unchanged state.

Deleting without loading from the database

You can delete the entity without loading from the database, provided you know the Primary key. The following example shows how to delete without loading from the database. We create a new department entity and assign the 2 to DepartmentID. Next, we attach it to the context and set its state as Deleted. On SaveChanges the EF will delete the Department

Delete & ConcurrencyCheck

The above code will not work if you have enabled ConcurrencyCheck on the table. For example open the Department model and add the ConcurrencyCheck on the Name property as shown below.

Run the above code again and you will see the following exepction

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll

Deleting Multiple Records

You can use RemoveRange method of DbSet to remove multiple records at a time. The following example code deletes 3 departments from the database in the connected scenario.

The EF sends one delete query at time to the database. So if you delete 100 records, the EF will issue 100 delete statements

The following code deletes the multiple records in disconnected scenario.

Reference

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top