-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
Problem:
I need to execute code when a new entity gets added to the dbContext/ChangeTracker but before the EF Core base code runs.
One way which I am currently using is that I derive and override the add-methods on some classes (dbContext, dbset, etc. etc. ). I execute my code and then call the base.AddXY() methods. This may work but is not a clear/nice solution because its way to complex to implement (code-generation manipulation while scaffolding) and new methods could be added and therefore needed to be overriden as well etc. so this approach is bound to break in the future.
Requested feature/solution:
A clear solution could be to introduce a new event to the ChangeTracker which gets fired when an entity is about to get tracked (before it actually gets added to the dbContext/ChangeTracker). An useful information on the eventargs could be why or from which source the entity is about to get tracked (add-operation, loading from DB, etc. ).
Right now there is only the "ChangeTracker.Tracked"-Events which gets fired when the entity got added (after all the base EF Core code has run) => https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.changetracker.tracked?view=efcore-5.0