-
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
Exception replacing multi-level relationship in EF7 #30135
Labels
area-change-tracking
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Milestone
Comments
Note for triage: I am able to reproduce this. In 6.0, the existing instances are cascade-deleted before the new instances are tracked. In 7.0, the identity conflict happens before the cascade delete. Could be related to #30122.
|
@iheb719 Workaround is to for the deletion before replacing the collection: public void UpdateParentChild(Parent parentUpdate)
{
var existingParent = _context.Parent.Local.FirstOrDefault(x => x.IdParent == parentUpdate.IdParent);
existingParent?.FirstChild.Clear();
_context.ChangeTracker.DetectChanges();
var parentBd = _context.Parent
.Include(x => x.FirstChild)
.ThenInclude(x => x.SecondChild)
.ThenInclude(x => x.ThirdChild)
.Single(x => x.IdParent == parentUpdate.IdParent);
parentBd.ParentName = parentUpdate.ParentName;
parentBd.FirstChild = parentUpdate.FirstChild;
_context.SaveChanges();
} |
Thanks @ajcvickers the workaround is working |
ajcvickers
added
the
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
label
Feb 3, 2023
ajcvickers
changed the title
Replace multi-level relationship in Entity Framework Core 6 vs 7
Exception replacing multi-level relationship in EF7
Mar 14, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-change-tracking
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Ask a question
I have the following model
I update Parent with all it's relationships it this way :
This works with EF Core 6 :
{newCollectionValuesList}
, there is an element that already exists inparentDb.FirstChild
(same ID), it get updated (with all its hierarchy){newCollectionValuesList}
, it get deletedWhen I upgraded to EF Core 7, I started to have this error :
I tried to add
.AsNoTracking()
in the query to resolve this, but my entities won't be updatedI don't want to loop manually through all the relationship and manually add the conditions to update the child elements
I didn't find anything related to that in What's new in EF7 nor in Breaking changes in EF7
This is a sample project with integration tests to reproduce the problem
Is this a bug ? because it works in EF6
Same result using dotnet6 and dotnet7
The text was updated successfully, but these errors were encountered: