-
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
Don't overwrite reference when querying for old principal #27497
Comments
@ulrich-berkmueller |
@ajcvickers thanks for the hint. It totally makes sense to me that the change tracker needs to be notified about the change. For me however it was unexpected that Further it feels strange that accessing the meta data with Anyway your recommendation is to always call |
@ulrich-berkmueller It's very unusual to use |
@ajcvickers Yes, the performance impact doing a full change tracking with And you can definitively say that the problem described above is not a bug but a taken decision (e.g. performance reasons)? Still, for me it doesn't feel good to silently overwrite an entity's relation that has not been loaded before even so it has been changed by the user ( So that others don't fall into the same trap, maybe one of the following can be done:
|
Not necessarily. Again, the docs I linked to go through various options. Notification entities could also be an option.
I will investigate if this particular case is something that could be made better with a targeted local DetectChanges. But regardless, the general principle still applies.
I think you need to design your application around this. It's not that common to do explicit loading at random places, but in those places if the model may have been changed, then a call to DetectChanges before calling Load would make sense.
Is there something missing from the change tracking docs that I have referenced?
This is an interesting idea. I'm not sure it really has enough value, but we will discuss. |
Yes, our "workaround" will be to add calls to Imagine you have an application that has an event system where you can inject plugin code. Let's say it is a webshop. You place an order and when placing the order the inventory needs to get updated which is happening in an inventory plugin. This should run in the same unit of work (transaction). Here you have multiple code bases using the same
Just the consequences if you miss to add the Thank you also for the time you spend or have spent on this! |
How to reproduce:
A working xUnit test project is attached (see TestProject.zip)
1.) Given an entity (e.g.
User
) with a relation property (e.g.Address
) and key property (e.g.AddressId
)2. ) And given some database entries
3.) Load the entity without loading the relation
4.) Load the new relation we like to assign to the entity
5.) Change the relation entity
6.) When I load the initially assigned relation entity
7.) Then the relation of the entity unexpectedly changes as well
Workarounds
The test project also includes two workarounds:
Workaround 1: Load the initial and new relation entity.
Workaround 2: Access the change tracker entry of the entity that contains the relation. Read-access is sufficient.
The following did not work (explicitly telling the
DbContext
that the entity has changed after assigning the new relation entity)Environment
The unexpected behavior was observed
The text was updated successfully, but these errors were encountered: