In reading an article on N-Tiered Applications, I came across information regarding concurrency tokens and change tracking information:
Another important concept to understand is that while the
default-generated entities support serialization, their
change-tracking information is stored in the ObjectStateManager (a
part of the ObjectContext), which does not support serialization.
My question is three-fold:
- Is there the same thing when using
DbContext? - If the only interaction with the database is in a
Repositoryclass within ausingstatement, does closing the database connection when the program leaves theusingstatement get rid of any option for change tracking? - Can this be leveraged as/with a Concurrency Token?
DbContextis just wrapper aroundObjectContextand it exposes change tracking information throughChangeTrackerproperty (returnsDbChangeTracker) and for particular entity through callingEntrymethod (returnsDbEntityEntry<T>).SaveChanges. It tracks changes you did on your entities since you loaded them into current context instance. Concurrency token resolves optimistic concurrency in the database => it validates that another process / thread / user / context instance didn’t change the same record your context is going to modify duringSaveChanges.