-
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
Client cascade delete for owned entities #10179
Comments
@ralmsdeveloper - This is different from #10180 because here there is no need of unique indexes. The issue here is, when you are trying to delete an entity it would remove the row from database. But since the table is being shared, there would be other entities using the same row so unless all of the entities sharing the row are deleted, deleting the row could be incorrect and cause data corruption. Hence before sending command EF Core tried to verify if all the entities in that row is marked as deleted and in the absence threw above exception. Though in this particular case, owned entities are sharing the table. If owner is deleted then owned entities should also lose its existence since it is tied with owner. If the entities are loaded in different state then its an error. But if it not then owner is deleted, owned children could be deleted too. Another use of table sharing is identifying 1-to-1 relationship. In that case, we need to think about the effects. For the case of principal getting deleted with cascade delete is enabled, we can delete but not for all cases. |
@smitpatel, thanks for the explanation, my intention is to help. so I'm trying to get more involved. I will try to analyze the cases more preventively. |
@ralmsdeveloper - Thanks for all the efforts. We really appreciate it. My intention was not to stop you from doing the good work. I wrote explanation on both just in case people may wonder in terms of why they are same or different. Further it will be helpful during triage meeting. |
I understand @smitpatel, Thank you very much. That makes it evolve. |
@silarmani Thanks for reporting this and also issues #10180 and #10182. There are some situations where EF currently uses more than just the key information for deletes. How much we want to allow stub entities like this to be used in these situations is something we will discuss as a team. Long term, these kinds of operations are probably best done with set-based operations, as tracked by #795. For now, one direction you could go is to use the FromSql method to write a set-based deletes manually. |
Notes from triage: we should delete owned types automatically in this case, but not types that are sharing the same table but without ownership. The reason for this is that for owned types, the delete is correct and safe by definition. For other types, it may not be, therefore for other types deleting a row requires that both entities are tracked and set as deleted. |
So now there's still no possibility to delete entity with owned type? |
@irmtim Currently you would need to delete the owned type explicitly. If you are getting errors open a separate issue. |
@AndriySvyryd How can deleting the owned type be achieved? |
@BennieCopeland usually it should be deleted when you delete the principal, but if not you can do something like this: context.Entry(entity).Reference(e => e.Owned).TargetEntry.State = EntityState.Deleted; |
I was having trouble with it earlier. I realized the issue I was having was because I gave the property a default value ( |
You can fix it with a simple model configuration. protected override void OnModelCreating(ModelBuilder modelBuilder) {
foreach (var relationship in modelBuilder.Model.GetEntityTypes().Where(e => e.IsOwned()).SelectMany(e => e.GetForeignKeys())) {
relationship.DeleteBehavior = DeleteBehavior.Cascade;
}
} Now, all the owned types (complex objects) will be deleted automatically when you delete the owner entity. |
Depends on #18960 |
I am trying to delete entities just by giving the PK however it fails if the entity contains Complex Type fields
Example
Example
It throws the following exception
Unhandled Exception: System.InvalidOperationException: The entity of 'BowtieCauseDetails' is sharing the table 'BowtieCauseDetails' with 'BowtieCauseDetails.Current#BowtieCauseTimeFields', but there is no entity of this type with the same key value that has been marked as 'Deleted'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.
If I provide empty place holder for those fields, it still fails but with a different message
Example
Unhandled Exception: System.InvalidOperationException: The property 'BowtieCauseTimeFieldsBowtieCauseDetailsNodeId' on entity type 'BowtieCauseDetails.Current#BowtieCauseTimeFields.Outgoing#BowtieNodeInOutFrequencyFields' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.
However if I get at least one field from the DB then it works
Example
I think it should work by giving the PK only.
Here is the full sample for easier replication
The text was updated successfully, but these errors were encountered: