dotnet / dotnet/EntityFramework.Docs
No mention of what happens if we attach with an entity ID that is already tracked?
- Dominant language
- Mermaid
- Stars
- 1.7k
- Forks
- 2k
- Avg merge
- 7d 23h
- Merged PRs (30d)
- 16
Description
### Type of issue
Missing information
### Description
Greetings.
I couldn't find any explicit mention of this scenario:
Let's say an entity *E1* with primary key *K* is attached (no matter how, no matter what *K* is and no matter *E1*'s [EntityState](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entitystate?view=efcore-9.0)).
Then, **another** entity *E2* that **has the same** primary key *K* is attached with a call to `Attach(TEntity)` or `Attach(Object)`).
If I extrapolate what's described in the documentation:
- *E2* should end up being attached.
- *E2*'s EntityState will be *Added* if *K* is the CLR default for the property type, or *Unchanged* if *K* is something else (see [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.attach?view=efcore-9.0)).
But what happens to the ChangeTracker's reference to E1? I guess it is replaced with one to E2? I think it would be worth explicitly stating what happens in this case.
Note that I couldn't find any information about this in these pages (it's possible I missed it though!):
- https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.attach?view=efcore-9.0
- https://learn.microsoft.com/en-us/ef/core/change-tracking/
- https://learn.microsoft.com/en-us/ef/core/change-tracking/explicit-tracking#attaching-existing-entities
### Page URL
https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.attach?view=efcore-9.0
### Content source URL
https://github.com/dotnet/EntityFramework.ApiDocs/blob/live/dotnet/xml/Microsoft.EntityFrameworkCore/DbContext.xml
### Document Version Independent Id
db1657b7-d390-7a27-062c-408e166a85ca
### Platform Id
5236a0ac-0a7a-2052-3a45-24d4543bb1b7
### Article author
@dotnet-bot
Contributor guide
Assessment
This issue has not been assessed yet.