Reputation: 69260
As part of some tests of Entity Framework Code First I'm testing the change tracking. In a small test database I have two cars in a table that I run a test method against:
Debug.WriteLine("Reading cars...");
var cars = context.Cars.ToArray();
Debug.WriteLine("Updating top speed of first car...");
Debug.WriteLine(string.Format("Type of car[0] is {0}", cars[0].GetType().ToString()));
cars[0].TopSpeed = 260;
Debug.WriteLine("Saving changes...");
context.SaveChanges();
I've added trace output to the getters and setters of the TopSpeed
and Brand
properties of the Car
class to see how they are accessed. TopSpeed
is an int?
and Brand
is a navigation property to the Brand
entity. Running the above code gives the output below.
Reading cars... Setting TopSpeed to 210 for ABC123. Car: Getting TopSpeed for ABC123. Setting TopSpeed to 250 for XYZ987. Car: Getting TopSpeed for XYZ987. Updating top speed of the first car... Type of car[0] is System.Data.Entity.DynamicProxies.Car_18E3E11297DC48759312BDF1C2FFEBE9F19BAE5D487CED2A9781A6CA730071EA Setting TopSpeed to 260 for ABC123. Saving changes... Car: Getting Brand for ABC123. Car: Getting Brand for XYZ987. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for XYZ987. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123.
The type of the object is an EF dynamic proxy for change tracking. Still, when calling SaveChanges()
the properties of the unchanged XYZ987 car are read. I thought that change tracking would cause EF to only read the objects that were known to change, or am I missing something? Is there something else I need to add to enable change tracking?
Upvotes: 0
Views: 1346
Reputation: 7523
There is a bug (or rather, lack of an optimization) in EF 4.1, 4.2, and 4.3 running on .NET 4 which causes DetectChanges to process entities that should be known to not need change tracking. This is because EF running on .NET 4 does not keep track of whether or not there are any entities that need change tracking or not, so it has to check each time.
This is fixed on EF5 when running on .NET 4.5, which will be available in beta very soon now. I just verified that with EF5 on .NET 4.5 the getters for properties of unchanged change-tracking proxies are not called in SaveChanges, even with AutoDetectChangesEnabled set to true.
That being said, even on EF 4.1-4.3 I don't believe that the getter should be called if all EF is doing is checking whether or not the entity is a change tracking proxy, so it seems like there might be another bug here.
Upvotes: 3
Reputation: 69260
Even though all requirements for a change tracking proxy was fulfilled, EF still uses the automatic change tracking unless explicitly turned off:
context.Configuration.AutoDetectChangesEnabled = false;
The documentation on AutoDetectChangesEnabled
is confusing:
True if the automatic detection of changes in the configuration is enabled; otherwise, false.
Looking at the ADO.NET's blog they explain it correctly.
Upvotes: 3
Reputation: 32437
To enable dynamic change tracking you need make all your scalar(non navigational) properties virtual.
Upvotes: 2