-
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
Strange Behaviour on detaching entries #17784
Comments
This is a change in behavior for 3.0 You can mitigate it: context.ChangeTracker.CascadeDeleteTiming = CascadeTiming.OnSaveChanges;
context.ChangeTracker.DeleteOrphansTiming = CascadeTiming.OnSaveChanges; |
I was under the impression that detaching is a completely seperate action to deletion. Doesn't the detached state only relate to the entity not being tracked? I am only intending to stop the context from tracking these entries, not modify anything about them, or their relationships. Also that change has been in since preview 3 according to the docs, but the change I've experienced has only happened since preview 9. |
I have just upgraded from preview-6 to preview-9 and am hitting this problem. I am caching user permissions, using the UserManager to get a user's role membership and then detaching the user from the change tracker. Rather alarmingly, a short while later and the user's role membership is deleted from the DB. The log shows: The 'IdentityUserRole' entity with key '{UserId: ..., RoleId: ...}' tracked by 'ApplicationDbContext' changed from 'Deleted' to 'Detached'. I can confirm that the mitigation steps outlined above do get around the problem but agree that this is some sort of regression as I didn't have the issue when I was on preview-6.
|
I've come across this too. Detaching entities after saving will now remove their nav props. This causes me to have to re-retrieve them. I wish there were a way of turning off tracking globally, not just for queries, but also for saving. I've never used EF's tracking capability. I consider it a hassle that gets in my way. I never want to run into any entity tracking collissions, so no tracking for me. I have an Update function in a generic repository, which I use to call Update(), SaveChanges() and then DetachAll(). I want to be able to send in whatever entity I like, in whatever state I like, at any time I like. I want it to come out saved to the database and still having its nav props, so I can continue to do more things with it in the calling code. Real life use case I am currently dealing with: A 'Package' entity is an entity that defines a package of graphical social media template designs. A user can view a package page. Viewing this must also increase the NrViews property on the package. Serverside, it should go like this:
As of EF3, I now have to do it like this:
Strange observation related to this new behavior:
Now drag the debug pointer back to 1, and then this happens:
Why would EF3 behave differently the 2nd time of doing the exact same thing? This leads me to believe something buggy is going on. |
+1 Detaching an entity from the change tracker should not trigger a cascading delete. This behavior does not seem to align with the description of the change in the documentation. |
@tom-mckinney You can turn that part off. The solution is posted in this very thread. It's the removal from the nav props that's grinding my gears. |
…t dramatically change detach behavior Fixes #17784 In 3.0 we made a [simple change](7e65b82#diff-7767de8ee880eb46efbfc9d2153dd33a) for #15645 that started clearing navigation properties when they referenced detached entities. In retrospect this was the wrong thing to do as people are relying on this to be able to detach all entities from the context. The new fix restores the pre-3.0 detach behavior, and sends the reload of a deleted entity through a simulated to delete instead.
I have just submitted a PR that reverts the behavior change in 3.0 where navigation properties are cleared when they reference an entity that is detached. That being said, detaching entities on mass is a very slow process--much slower than creating a new context instance. If creating a new instance is too hard based on other design constraints in the application, then #15577 when implemented would be a better choice. |
@jwbats Following up your comments here and on #18406. Considering this scenario:
This implies that you are first using a no-tracking query, then attaching the graph with
I never trust this. How do you really know what the state of everything is?
Not completely. Once |
…t dramatically change detach behavior Fixes #17784 In 3.0 we made a [simple change](7e65b82#diff-7767de8ee880eb46efbfc9d2153dd33a) for #15645 that started clearing navigation properties when they referenced detached entities. In retrospect this was the wrong thing to do as people are relying on this to be able to detach all entities from the context. The new fix restores the pre-3.0 detach behavior, and sends the reload of a deleted entity through a simulated to delete instead.
On detaching entries from the change tracker, I am getting an Invalid Operation Exception. It seems that detaching entries from the tracker is setting foreign keys or parents to null in child objects. In the past(preview 8) detaching has only stopped tracking the entries, without modifying/altering any properties.
Note: Nullable Reference Types is not enabled in this project
Steps to reproduce
Add multiple linked entries to a dbcontext using Update.
SaveChanges.
Clear entries with:
Relevant modelbuilder code:
Exception
Further technical details
EF Core version: 3.0.0-preview9.19423.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 64 bit
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: