There are a few events in OpenAccessContext.Events
that can be used to implement the feature that you want, but I am not sure that any of them will be really helpful. They operate on data that will be potentially persisted in the database. You can always create a new context and ignore the existing one or just call ClearChanges
. Having that in mind I will not use Added for this purpose at least without introducing some smart filtering of events and safeguards.
From my point of view the only good place to hook is OpenAccessContext.SaveChanges()
. If you want to have the unique identifier of the invoice and it is generated by the database that you should raise the event immediately after SaveChanges()
is called so the IDs will have their real values.
You can implement that extending your OpenAccessContext
in the following way:
// get a snapshot of the current changes to be persisted
var changeSet =
// get all newly added invoices
var addedInvoices = changeSet.GetInserts<Invoice>();
// refresh the entities in memory
// use the invoice items as you like
Please note that there will be some overhead cause by the creation of the change set snapshot each time SaveChanges()
is called. You should weight this against the option to have the business logic in all pages where Invoices are created.
should be called after a new entity instance is added to the context for the first time:
var context =
Invoice invoice =
// Added is not called yet!
// set some properties to the new instance...
// context.Events.Added is raised
If you are adding the Invoice to a related entity instance (acting as child in One-to-Many relationship), make sure that IsManaged
setting of the association is set to True
. Otherwise the event will not fire in this case.
If you suspect that Adding/Added
events are not raised as they should be, please send us a code snippet that demonstrates the behaviour.
the Telerik team