I’m still catching up on all the improvements that come with the release of .NET 7. One thing I noticed is a nice new feature in EF Core 7.
Before EF Core 7 when you want to delete an entity you had to fetch it first. With the new ExecuteDelete
(and ExecuteUpdate
) method you can execute delete commands (and update commands) directly in the database.
Executing the code above will result in the following query:
As you can see, it generates a SQL statement to delete the entities that match the condition. Exactly what you would expect and without the overhead of loading the entities in memory first. Nice!
Remark: EF Core 7 can be used with .NET 6. So you can start using this feature without having to upgrade to .NET 7 first.
Also keep the following in mind when using the ExecuteDelete
method:
- The specific changes to make must be specified explicitly; they are not automatically detected by EF Core.
- Any tracked entities will not be kept in sync.
- Additional commands may need to be sent in the correct order so as not to violate database constraints. For example deleting dependents before a principal can be deleted.