Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change tracking works in Entity Framework

Given the following code, how does EF/DbContext knows about the change made to the customer object:

class Program {     static void Main()     {         using(var shopContext = new ShopContext())         {             var customer = shopContext.Customers.Find(7);              customer.City = "Marion";              customer.State = "Indiana";              shopContext.SaveChanges();         }     } }  public class ShopContext : DbContext {     public DbSet<Customer> Customers { get; set; } }  public class Customer {     public int Id { get; set; }     public string FirstName { get; set; }     public string LastName { get; set; }     public string City { get; set; }     public string State { get; set; } } 

Thank you

like image 596
Yair Nevet Avatar asked Apr 30 '12 01:04

Yair Nevet


People also ask

How does Entity Framework track changes?

EF Core change tracking works best when the same DbContext instance is used to both query for entities and update them by calling SaveChanges. This is because EF Core automatically tracks the state of queried entities and then detects any changes made to these entities when SaveChanges is called.

What is EF core change tracking for?

EF keeps track of all the changes applied to all the entities and their properties, so that it can build and execute appropriate DML statements to the underlying data source. An entity at any point of time has one of the following states which are represented by the enum Microsoft. EntityFrameworkCore.

How do I turn off change tracking in Entity Framework?

If property AutoDetectChangesEnabled in DbContext. Configuration is set, EF runs change tracking according to rules described above. So you can disable it by setting this property to false.

What is change tracker?

Change tracking is a lightweight solution that provides an efficient change tracking mechanism for applications. Typically, to enable applications to query for changes to data in a database and access information that is related to the changes, application developers had to implement custom change tracking mechanisms.


1 Answers

When you load the entity from the context it keeps an additional data structure - let's call it entry. The entry contains two set of values - original values and current values. When you execute the SaveChanges operation EF goes through your customer entities and updates current values in the entry so that they match with the real state of your entity - this operation is called detecting changes. During SQL command generation EF will compare current and original values and build an SQL update statement to modify changed values in the database. This operation is called snapshot change tracking - EF keeps a snap shot in the entry.

There is an alternative called dynamic change tracking which will modify the current value in the entry at the same time you assign the value to your entity's property. Dynamic change tracking has specific requirements (like all of your properties in the entity must be virtual) because it must wrap your class to a dynamic proxy at runtime. This used to be the preferred way but due to some performance issues in complex scenarios, snapshot change tracking is currently supposed to be used as default.

like image 80
Ladislav Mrnka Avatar answered Sep 19 '22 01:09

Ladislav Mrnka