Updating and deleting objects

EF Core tracks objects' states through the ChangeTracker type. Every object that is retrieved from the database is automatically tracked by the ChangeTracker, and every change that is made on it changes the object's EntityState to Modified. If a retrieved object is removed from DbSet (or if an object that contains it is removed), EF Core will change the EntityState value to Deleted.  When EF Core SaveChanges is executed, it scans the tracked objects, and if they are modified or deleted, EF Core generates an UPDATE statement with the relevant modifications that are needed, and a DELETE statement for the deleted objects.  Here is a code snippet that shows how to update a product and then delete it:

var product ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.