Lazy-loading collections with current state from the database
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Bug description
I'm attaching a test that reproduces the issue. Run the test and the test passes. Uncomment the commented line, and the test now fails. This is hitting hard as depending on the order I'm accessing the entities; I'm getting more or less items in my collections.
I believe I understand why this happens, but I think the behavior for collections is wrong. If I'm accessing a lazy-loaded collection, I expect EF to query/load that collection from the DB, and not try to infer, by some obscure relationships, if some entities are already in memory and populating that collection with what's in memory.
For single-entity navigations it's ok, since you have the existing entity by Id, or doesn't. But for collections this behavior is unacceptable.
[EfTest.zip](https://github.com/user-attachments/files/23477444/EfTest.zip)
### Your code
```csharp
Code is attached as test project.
```
### Stack traces
```text
```
### Verbose output
```text
```
### EF Core version
9.0.10
### Database provider
Microsoft.EntityFrameworkCore.Sqlite
### Target framework
_No response_
### Operating system
_No response_
### IDE
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.