Question

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?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top