How does DetectChanges work?

There are the following 2 objects with the following properties:

Parent
    ID
    Children
Child
    ID
    ParentID
    Parent

Now I have the following code:

db.Configuration.AutoDetectChangesEnabled = false;

var child1=new Child();
parent.Children.Add(child1);
db.ChangeTracker.DetectChanges();
parent.Children.Remove(child1);

var child2=new Child();
child2.Parent=parent;
child2.ParentID=parent.ID;
db.Children.add(child2);

At this point, child1 and child2 are completely identical. The properties of the parent and parent object have the same value (value parent). Studying dbContext entryfor both of them also shows exactly the same information, for example. OriginalValues ​​is empty for both.

If I call now db.ChangeTracker.DetectChanges, then child1.Parent becomes zero, and child2.Parent retains its value. How does EF know about this - where does it store the information necessary to be able to make this distinction?

Thanks for any ideas.

+4
source share
1 answer

, , , .

Entity Framework

ObjectStateEntry , ObjectStateManager.

ObjectContext.DetectChanges

, , context.ChangeTracker.DetectChanges ( ). , DetectChanges, (Remove Add) , . , child1 parent.Children.Remove(child1) child2 , , ().

, , - performace:

, , , .

/ , :

.NET Framework > DetectChanges

+1

All Articles