Modifying Data with EF Core: More Efficient Updates and Deletes
Explore how to improve data modification efficiency in Entity Framework Core by using ExecuteUpdate and ExecuteDelete methods. Learn to update and delete records without loading entities into memory, and understand best practices to maintain context synchronization while improving performance.
We'll cover the following...
The traditional way to modify data with EF Core
The traditional way of modifying data using EF Core is summarized in the following steps:
Create a database context. Change tracking is enabled by default.
To insert, create a new instance of an entity class and then pass it as an argument to the
Addmethod of the appropriate collection, for example,db.Products.Add(product).To update, retrieve the entities we want to modify and then change their properties.
To delete, retrieve the entities we want to remove and then pass them as an argument to the
RemoveorRemoveRangemethods of the appropriate collection, for example,db.Products.Remove(product).Call the
SaveChangesmethod of the database context. This uses the change ...