-
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
Lazy loading detached items when using reinitializing #13264
Comments
@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. |
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.) |
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. |
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. |
https://stackoverflow.com/questions/52236367/list-clear-vs-new-list-asp-net-core-entity-framework-core-lazy-loadi
The text was updated successfully, but these errors were encountered: