Skip to content
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

Closed
marcelgoldstein opened this issue Jan 3, 2022 · 2 comments · Fixed by #28297
Closed

ChangeTracker: New event for "entity about to get tracked" #27093

marcelgoldstein opened this issue Jan 3, 2022 · 2 comments · Fixed by #28297
Labels
area-change-tracking area-interception closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@marcelgoldstein
Copy link

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

@optiks
Copy link

optiks commented Jan 6, 2022

Can you elaborate on why you need to run code before tracking?

@marcelgoldstein
Copy link
Author

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.
When instantiating an entity I populate the composite key properties except the ID property because it needs to be set to a unique value and therefore gets calculated by my base-code (not in each viewmodel) when adding it to the DbContext. The calculation of this ID value cant be the final one used on the SQL server because it maybe saved later (and having multiple changes from other places on the database), so the ID value gets an just for the ChangeTracker uniquely value at this point (decrementing from -1).
The whole composite key needs to be unique when adding the entity to the DbContext because EF Core snapshots the "identity" of the entity at this point and therefore would throw an exception if a second entity with the same composite key values would get added.
This "identity-snapshot-mechanic" is the reason I need to be able to execute base-code before it gets added to the ChangeTracker. Otherwise my entities would not have an unique identity from the ChangeTracker perspective and it would throw exceptions.

I hope that makes sense.

ajcvickers added a commit that referenced this issue Jun 22, 2022
- Tracking
- StateChanging
- DetectingChanges
- DetectedChanges

Part of #626
Fixes #27093
Fixes #16256
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 22, 2022
ajcvickers added a commit that referenced this issue Jun 24, 2022
- Tracking
- StateChanging
- DetectingChanges
- DetectedChanges

Part of #626
Fixes #27093
Fixes #16256
ajcvickers added a commit that referenced this issue Jun 24, 2022
- Tracking
- StateChanging
- DetectingChanges
- DetectedChanges

Part of #626
Fixes #27093
Fixes #16256
ajcvickers added a commit that referenced this issue Jun 29, 2022
- Tracking
- StateChanging
- DetectingChanges
- DetectedChanges

Part of #626
Fixes #27093
Fixes #16256
ajcvickers added a commit that referenced this issue Jun 29, 2022
- Tracking
- StateChanging
- DetectingChanges
- DetectedChanges

Part of #626
Fixes #27093
Fixes #16256
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview7 Jul 7, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview7, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking area-interception closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants