To track changes to the Context you can use the ObjectStateManagerChanged
event of the ObjectStateManager
. To access the ObjectStateManager
, you have to use the IObjectContextAdapter
for casting the DbContext like
var contextAdapter = ((IObjectContextAdapter)dbcontext);
contextAdapter.ObjectContext
.ObjectStateManager
.ObjectStateManagerChanged += ObjectStateManagerChanged;
Once you got the event, it fires every time the collection gets changed by adding or removing entities to the ObjectStateManager
.
To track the state of the entity, use GetObjectStateEntry()
of the ObjectStateManager
and use the Element
of the CollectionChangeEventArgs
param.
Combining both states of CollectionChangeEventArgs
and ObjectStateEntry
you can track, what is going on....
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…