-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChangeTracker: New event for "entity about to get tracked" #27093
Comments
Can you elaborate on why you need to run code before tracking? |
I have SQL tables with composite primary keys. Some of them also have a column ID which is the last of the columns participating in the composite key. This ID column gets incremented, but not by the SQL server (identity column with auto-increment) natively. It gets its value from the code executed on SaveChanges just before the EF Core code generates the SQL and runs it on the SQL server. I hope that makes sense. |
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
The text was updated successfully, but these errors were encountered: