EF 4.1 RC. I want to run some code after an entity has been added/attached to the DBContext. Is there an event for this (I can't find one). Basically I want to check if the added/attached entity is of a certain interface and if it is, do some stuff with it. Thanks!

- 360,892
- 59
- 660
- 670

- 9,363
- 16
- 67
- 91
-
As Ladislav said, there's no extensibility. If you are looking for something more powerful (and not much more complicated, if you're willing to learn) I recommend NHibernate. It does what you want and more. – Diego Mijelshon Mar 26 '11 at 17:07
3 Answers
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....
-
It seems nice, however in my testing, ObjectStateManagerChanged actually gets called _before_ anything is saved to the database (it is emitted when you call Add()), and ObjectContext.SavingChanges somewhat obviously gets called before saving as well. I haven't found anything that gets called _after_ the changes are saved... (in EF6) – Josh Sep 24 '14 at 16:19
-
@Josh which is one reason for providing your own e.g. "unit of work" wrapper around EF - it allows you do manage when save changes is called, vs when commit is called (if you've opened you own transaction). However, for my circumstances, the state changed manager looks to provide exactly what I need. – Nathan Aug 17 '15 at 15:00
Unfortunatelly there are no such events available and there are no extension points to add such events. That is in my opition one of the biggest EF failure. The extensibility = zero.
The only thing you can do is override SaveChanges
and before executing base.SaveChanges
use ChangeTracker
to get all attached and added entities of your type and execute your logic. But it is not the same as triggering an event when attaching or adding an entity to the context.

- 360,892
- 59
- 660
- 670
-
I'm not testing against v 4.1 as indicated in original question, but @KVerwold's answer is just fine below! Just tested the use of the ObjectStateManager in EF5 and it works great. – BenSwayne May 09 '13 at 19:14
-
This seems to be the only way to hook in _after_ changes have been saved to the database. – Josh Sep 24 '14 at 16:26
Handle the CollectionChanged event for the relevant DbSet's Local property (ObservableCollection).
Check the added/attached entity object's DbEntityEntry's state for added or unmodified for added/attached, respectively.
DbSet.Local property: http://msdn.microsoft.com/en-us/library/gg696248(v=vs.103).aspx
DbContext.Entry method: http://msdn.microsoft.com/en-us/library/gg696578(v=vs.103).aspx

- 15,457
- 1
- 30
- 31
-
Note that this does work and, would have been a great method to use for me, however it emits the event _before_ the entity is actually saved to the database, so it might trigger a "false positive" if something went wrong. – Josh Sep 24 '14 at 16:09
-
The question asked about processing entities of a certain type after they were added/attached to the `DbContext`. If you want to perform further processing after entities are saved to the DB successfully, override the `DbContext.SaveChanges` method and implement your logic after you call the `base.SaveChanges` method. – Moho Sep 25 '14 at 22:36