Description
Removing an item from a collection mapped as extra-lazy has sometimes no effect: the item stays in the collection.
This happens when the remove is done while the collection is not yet initialized, and if it is enumerated after the remove. In such case, the enumeration not only yields the item, but also at later flush, the item is not deleted from the collection.
Corresponding Hibernate issue: https://hibernate.atlassian.net/browse/HHH-5742
Initial report follows:
Not sure if this is a bug or something I am doing but I have an entity with a child collection, exposed as an IEumerable<> and I am having issues removing the item:
public class Parent {
private ISet<ChildItem> _childItems;
public virtual IEnumerable<ChildItem> ChildItems
{
get { return _childItems; }
}
public virtual void RemoveChildItem(ChildItem item)
{
...
_childItems.Remove(item);
...
}
}
I do the following to remove the item:
myChildItem.Parent.RemoveChildItem(myChildItem);
If I stick a breakpoint after the _childItems.Remove(item);
line then I discover that the _childItems collection still contains the item.
I think it has something with initialising the _childItems collection (extra lazy load) because if I add a check before it like:
if (!_childItems.Any(c => c == item)) throw new Exception();
then the _childItems.Remove(item);
works correctly. Likewise if I put a breakpoint before the remove and inspect the collection (causing it to initialise), then it works.