Sobes.tech
Junior — Middle

Can you explain what change tracking in Entity Framework Core means?

sobes.tech AI

Answer from AI

Change tracking in Entity Framework Core is a mechanism by which the context monitors the state of objects (entities) loaded from the database. When you load an entity, EF Core starts tracking its changes: adding, updating, or deleting properties.

This allows the SaveChanges() method to automatically determine which specific changes need to be applied to the database.

Example:

using(var context = new AppDbContext())
{
    var user = context.Users.First(); // entity starts tracking
    user.Name = "New Name";       // EF Core detects change
    context.SaveChanges();           // updates only the changed field in the database
}

If tracking is disabled (for example, using AsNoTracking()), EF Core will not track changes, and you will need to manually manage entity states to save changes.