-
Notifications
You must be signed in to change notification settings - Fork 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's StateChanged event's changes not being updated #3888
Comments
I should also point that calling |
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
The code supplied is enough to see what is happening is it not? It this information is not enough you can tell me what more is needed and I will provide |
@patrickhacens There is no code for the DbContext type or entity types. |
@ajcvickers Sorry for that, something must have happened when I pasted it, will be uploading shortly |
I updated the snippet with DbContext and entity types, if anything else is missing please ping me. If it really is not the intended behavior I would be apt to delve deeper and do a pull request |
Note for triage: the issue here is that the event is happening as part of DetectChanges, which means if a property value is set while processing the event, then it won't be detected until DetectChanges happens again. This isn't desirable, but it's not immediately clear to me how to solve this. It's related, but in some respects the opposite of, dotnet/efcore#26506. |
We discussed this with the team, and there really doesn't seem to be any way around this. Doing another round of DetectChanges would potentially lead to the need for yet another, and so on, as well as being a big performance hit. Instead, the way to deal with this is to set the property value in a way that EF knows about it without having to detect it. For example: switch (e.Entry.State)
{
case EntityState.Modified:
case EntityState.Added:
e.Entry.Property(nameof(Entity.ChangedAt)).CurrentValue = DateTime.Now;
break;
} This is something it would be worth noting in the documentation. |
File a bug
Base on this documentation I expect that when saving changes any property manipulation inside the
StatusChanged
event would also be included in the update command generated by EF, done some tests and I can't get it to work, and I'm not sure if that is the expected behavior.Testbed
Below is a test snipped that can be run in a toplevel statement.
At the set up value on the database part the ChangedAt property is properly set using the Tracked event.
Simulating normal update entity use case, I retrieve the value from a new context and change its name property, after calling SaveChanges the StatusChanged event is callend and the entity ChangedAt property is updated, but this update is not persisted on the database (or not present in the sql generated when using a SQLServer provider), but still the entity's ChangedAt property is de facto changed.
After that for control I retrieve the same entity again to be sure that the ChangeAt property is not updated in the previous step.
OUTPUT
Include provider and version information
EF Core version: 6.0.5, and 6.0.4
Database provider:
Microsoft.EntityFrameworkCore.SqlServer
andMicrosoft.EntityFrameworkCore.InMemory
Target framework: .NET 6.0
Operating system: win10-x64 build 19044.1645
IDE:
Microsoft Visual Studio Community 2022 (64-bit) 17.3.0 Preview 1.0
The text was updated successfully, but these errors were encountered: