Entity Framework Core Interview Question #19
What does SaveChanges or SaveChangesAsync do?
EF Core Fundamentals & DbContext Junior Beginner
Quick Interview Answer
SaveChanges examines tracked entity states and sends the required INSERT, UPDATE, and DELETE commands to the database. If successful, EF Core updates tracking state to reflect the persisted changes.
Detailed Explanation
SaveChanges examines tracked entity states and sends the required INSERT, UPDATE, and DELETE commands to the database. If successful, EF Core updates tracking state to reflect the persisted changes.
SaveChangesAsync performs the database I/O asynchronously and is normally preferred in ASP.NET Core request processing.
A single SaveChanges call can persist multiple tracked changes. Developers should define transaction boundaries according to the business operation rather than calling SaveChanges after every property assignment.
Code Example
db.Products.Add(product);
db.Categories.Add(category);
await db.SaveChangesAsync(cancellationToken);