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

Lazy loading detached items when using reinitializing #13264

Closed
johnybravo666 opened this issue Sep 8, 2018 · 4 comments
Closed

Lazy loading detached items when using reinitializing #13264

johnybravo666 opened this issue Sep 8, 2018 · 4 comments

Comments

@johnybravo666
Copy link

https://stackoverflow.com/questions/52236367/list-clear-vs-new-list-asp-net-core-entity-framework-core-lazy-loadi

@ajcvickers
Copy link
Member

@johnybravo666 In this code:

FooParentEntity fooParentEntityDb = fooDbContext.FooParentEntities.ToList().FirstOrDefault();
fooParentEntityDb.FooChildEntities = new List<FooChildEntity>();
fooParentEntityDb.FooChildEntities.Add(new FooChildEntity { Name = "ThirdChild" });

The first line loads parent entities; child entities are not loaded. The second line sets child entities to a new empty list, but since the child entities are not loaded this is essentially a no-op. The third line then lazy-loads the child entities, since they have not been loaded, and then adds a new child entity to the list.

The reason it behaves differently in the debugger is because when the debugger accesses the FooChildEntities property is causes the property to be lazy-loaded.

@ajcvickers
Copy link
Member

Question for triage: should setting the property to a new empty collection cause it to be marked as loaded and empty? (Note that this is difficult--it likely means that the setter will have to trigger lazy loading, because otherwise we would not know which entities to mark as deleted.)

@ajcvickers
Copy link
Member

ajcvickers commented Sep 10, 2018

Triage: we will consider for 3.0 that if a proxy is tracked, then calling the setter should cause all entities to be loaded and then marked as deleted before setting the new empty collection. We can do something similar for references. Also, consider using the EntityEntry APIs as a back door mechanism for manipulating a collection without loading it.

@ajcvickers ajcvickers added this to the 3.0.0 milestone Sep 10, 2018
@ajcvickers ajcvickers self-assigned this Sep 10, 2018
@ajcvickers
Copy link
Member

Notes from planning. We discussed this again with reference to #14086 and will likely not implement this because of the issues mentioned there. In addition, it's hard to know that setting an empty collection is really intended to do this, as opposed to just setting the collection so that things can be added, even if the setter is called after the entity is already tracked.

@ajcvickers ajcvickers removed this from the 3.0.0 milestone Jan 25, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants