Repository navigation
[EF Core 8] [Breaking Change] - error on working with explicit many-to-many relations with OnDelete.Restrict #32383
Description
Activity
Note for triage: everything seems to be by-design here.
Reacted by Andrej KrižanWe also ran into this same breaking issue on our 7 --> 8 upgrade
We found this problem after upgrading our project from EF Core 7 to EF Core 8.
Hope, the code mentioned by @Gretam11 is corrent in EF Core 7 and also corrent in EF Core 8. Even if it is not purely clean it became usual.This breaking change (if by-design for triage) is not mentioned at Breaking changes in EF Core 8.0.
Reacted by Andrej Križan, mlaflamme49, Michal Turecki and Kadir AvcıWhy the code is correct? The same code is mentioned in the Severing a relationship chapter of the documentation.
Severing a relationship now works only for DeleteBehaviors Cascade and ClientCascade. For all other DeleteBehavios the change tracker throws an exception. The exceptions say we cannot change or modify the value of the (composed) primary key, but we are not trying to do it, we want to delete the entity representing the many-to-many relationship.
The only workaround I found is to registrer entities to be removed prior to removing them from the collection.
dbContext.RemoveRange(book.BookAuthors); book.BookAuthors.Clear();Reacted by Scantraxx123, rofenix2, mlaflamme49 and Kadir AvcıI also have this regression but only on join tables with payload.
It seems that just declaring the relationships as Cascade in the EF model is another possible workaround. You don't have to actually apply any change to the database tables. Ugly but it works.
10 remaining items
Why is this closed if the fix doesn't come until version 8.0.2?
@daemons88 Because all the work is done and the fix is now ready to ship with 8.0.2.
If you need it now you can use the nightly builds.
Reacted by MarcoOkay, thanks for your answers!
Will the fix also work if the entity has a composite key with references to the same table?
I am having the same issue with the following table:public class RoleInRole { [Key, Column(Order = 0)] public int RoleId { get; set; } public Role Role { get; set; } [Key, Column(Order = 1)] public int InRoleId { get; set; } public Role InRole { get; set; } public RoleAccess Allow { get; set; } }@ajcvickers When do you plan to release version 8.0.2
Reacted by MarcoWould like to know the same! This "issue" is affecting a solution of mine, but there is no daily build with this fix available, so I'm also eagerly awaiting the official release of 8.0.2 :)
@LennardF1989 The latest daily build contains this fix. If you are seeing otherwise, then please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
That's an EFCore 9 version, no? I would like to stick to the 8.x range :) The dotnet8 feed doesn't go further than 8.0.0.
However, I just tried to workaround mentioned by @jirikanda and can confirm that first running a Remove/RemoveRange on the DBContext prior to removing it from a navigation property does the trick as well!
Reacted by Jiří Kanda- marked After update to .net 8: Error System.InvalidOperationException : The property 'Id' 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', and then associate the dependent with the new principal. #32588 as a duplicate of this issue
on Jun 16, 2025 - added a commit that references this issue
on Dec 10, 2025
Description
I'm currently migrating our project to the latest .NET and EF Core version (8), and I stumbled across the narrow, undocumented breaking change.
When you have many-to-many relation (
Authors<->Books) with explicit intermediate table (BookAuthors), and you defined relations withOnDelete.Restrictaction, thenSaveChanges()throws an error if you try to modify list of linked entities (BookAuthors) through the root entity (Book).Example
Entities:
Model config:
Actual operation:
Exception:
Versions information
EF Core version: 8.0.0
Database provider: ANY
Target framework: .NET 8.0
Additional information
Considering that normal one-to-many relations update with
OnDelete.Restrictwould also fail in such scenario (with clearer message though:The association between entity types 'Book' and 'Author' has been severed, but the relationship is either marked as required or is implicitly required), probably, the behavior above is correct, and it was wrongly working in EF Core v7. But I decided to create a ticket anyway, maybe it'll help someone who also stumbles across the same issue (since it's kind of a breaking change).Reproduction repository
Here you can check the minimal project where this bug is reproduced (link).
You can also change package versions back to EF.* v7 to check that it wasn't the case in v7.