-
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
Entity already tracked exception for entity with multiple references to same navigation property #20116
Comments
Don't know if somehow this is also related to this issue: |
Sounds like the same issue. I can provide both projects, one .net core 2.0 and one .net core 3.0 that produces this issue if needed. I think EF Core should be able to detect that the same primary key should map to the same entity. |
This is not supported because in general resolution of conflicts between two or more different instances in the same graph is very ambiguous. Property values may be different, but more importantly, for anything other than a single entity with no relationships, the connections in the graph are also different. |
See also #20124 |
Is there a workaround for this case
? |
What in the case that in two different places in graph we are using exactly the same instance of entity. There is no way to have other properties and relation in such case, right ? I think I should not throw exception in such case. |
@buttch The exception is only thrown when more than one instance is found. |
@ajcvickers you are right. I'm sorry for confusion. I had to double check. |
When trying to attach/update an entity with multiple references to the same entity (and included with
Include
/ThenInclude
), EF core throws an exception indicating the entity is already tracked.For example:
I can work around this by using a new context to materialize the entity and then making the changes on that entity; or using
context.Entry(rental).Reload()
; however, is it possible tocontext.Attach
orcontext.Update
disconnected entities with multiple references to a same entity? Why or why not?Steps to reproduce
Alternatively, trying to seed the data with multiple references to the same User also raises the exception:
Can EF core detect the same primary key and treat the entity as the same for tracking purposes?
Further technical details
EF Core version: 3.1.2
Database provider: npgsql
Target framework: .net core 3.1
Operating system: Win 10
IDE: vs 2019
The text was updated successfully, but these errors were encountered: