Skip to content
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

Multi-level entities update throws another entity with same key value already tracked exception #22067

Open
1 task done
baibai-cbd opened this issue Feb 3, 2025 · 1 comment

Comments

@baibai-cbd
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Description

I have a ClassA which has a list of ClassBs, ClassB with a list of ClassCs, with parent child relations in dbcontext.
I have configured Dtos and CreateUpdateDtos for above classes to follow the usage of CrudAppService class.
When using the inherited endpoint in CrudAppService to update the entities, would throw exception about entity tracking

Reproduction Steps

https://github.com/baibai-cbd/playground.hierarchy
sample repo to reproduce the issue

  1. launch app
  2. go to swagger, go to ClassA POST endpoint
  3. create a new ClassA entity with one child ClassB entity and one child ClassC entity within classB
  4. copy the result data
  5. go to ClassA PUT endpoint, paste the above data as body, copy paste the id as path variable id
  6. modify any non-id value in the body
  7. execute the endpoint, got error result returned

Expected behavior

the multi-level parent-child entities should save gracefully without exception

Actual behavior

throws exception

The instance of entity type 'ClassC' cannot be tracked because another instance with the key value '{Id: 3a17dd15-6be3-06dc-5ebb-103bf221f4c3}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

Regression?

No response

Known Workarounds

No response

Version

9.0.3

User Interface

Common (Default)

Database Provider

EF Core (Default)

Tiered or separate authentication server

None (Default)

Operation System

Windows (Default)

Other information

I'm not sure if I'm doing a correct way writing these code, but when I use the similar code for a two-level parent child relationship entities, it is working nicely for me. So if this is not the correct way of doing it, please help me understand how to write the code for a three-level parent child relationship.

@baibai-cbd baibai-cbd added the bug label Feb 3, 2025
@baibai-cbd baibai-cbd changed the title Multi-level entities update throws another entity with same id already tracked exception Multi-level entities update throws another entity with same key value already tracked exception Feb 3, 2025
@maliming
Copy link
Member

maliming commented Feb 4, 2025

Can you share the value of your CreateUpdateClassADto object?

You can serialize it to JSON.

@maliming maliming removed the bug label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants