ObjectStateManager and Detached entity state

2.5k views Asked by At

I'm using Entity Framework and some parts of its functionality are still unclear for me. The main things that are hard to understand are:

  • SubmitChanges process;
  • Order of invoking update-delete-insert methods;
  • Understanding of ObjectStateManager
  • Detached EntityState and it's part in submitting process;

Provide your explanations or give some useful links.

1

There are 1 answers

3
Ladislav Mrnka On BEST ANSWER

ObjectStateManager is a component exposing information about tracked entities. Each entity which can be persisted by EF must be tracked = attached. Detached entities are unknown to EF (they are not tracked) and so their changes are not saved when you call SaveChanges (there is no SubmitChanges in EF). Tracking consists of maintaining information about initial state of the entity or relation and changes done to them. It also contains the global state of the entity.

The process when you call SaveChanges depends on the way how you configured EF to track changes, on the way how you got the entity and on the changes you did.

Change tracking is a feature which allows EF to track changes applied on entities attached to the context (each entity loaded by the query is by default attached). EF contains to version of change tracking:

  • Snapshot - only for POCOs. EF doesn't track changes to entities but when you call SaveChanges it compares stored state of the entity (obtained when the entity was loaded) and the current data in the entity and set its state accordingly.
  • Dynamic - native for EntityObject based entities and achieved by dynamic proxies for POCOs. Each change to attached entity triggers change in tracked entry and sets entity state accordingly. When you call SaveChanges are states are already set up.

The order of data modification operations is EF internal implementation. The basic order is defined by your mapping where dependency between entities is described.