Open DrPhil opened 1 year ago
A workaround for this is to not remove the entity from the old collection with oldBlog.Posts.Remove(post)
. If you need to access the posts on the old blog before SaveChanges has happened, use a filter: oldBlog.Posts.Where(p => p.Blog == oldBlog)
.
Note for triage: this is an interesting case where reparenting an entity with required children may cause the children to be deleted before the reparenting is complete.
This bug has not received so much attention, so I understand that it is low on the priority list. On the other hand this causes silent data-loss in a not so weird scenario. We were able to find one of the cases where this happens, but we think there are many more hidden in our codebase (and likely others too!). Any idea of if/when this would be prioritized?
This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 8.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.
File a bug
When we have several level of collection entities like this:
Blog
->Post
->Comment
, then you get inconsistent (and surprising!) behavior if you manually change what collection a middle entity is part of. Sometimes the children entities will be deleted, and sometimes they are kept. I would have assumed that they were always kept.The documentation gives a tip:
In the below example we manipulate both navigations and no FK, and we still get inconsistent behavior.
Include your code
Models.cs
Program.cs
This will delete the post comments and output:
The comment is in state Detached!
Changing program to fetch the blog before fetching the post will not delete the comment
and it will output:
Include stack traces
n/a
Include verbose output
n/a
Include provider and version information
EF Core version: 7.0.4 Database provider: I've reproduced with SqlLite and SqlServer. Target framework: net7.0 Operating system: Linux IDE: Rider