Skip to content

Rich collection support: Dictionary, Lookup, HashSet, etc. #2919

Description

@divega

Based on feedback at #246 (comment).

Activity

  1. added this to the Backlog milestone on Aug 25, 2015
  2. changed the title [-]Rich collection support, e.g. Dictionary, Lookup, HashSet, ordered collections, etc.[/-] [+]Rich collection support: Dictionary, Lookup, HashSet, ordered collections, etc.[/+] on Sep 1, 2015
  3. sjb-sjb commented on Jan 28, 2019

    @sjb-sjb

    During materialization, EF fixes up the navigation collections using standard interface properties e.g. ICollection<T>.Add. As mentioned in a comment in #12675 this is not very satisfactory because the ordering of elements is not preserved during a materialization -- a point that obviously only surfaces for ordered collections. Another way to state this is that the user would like to have Add with the normal semantics of "add at end" while redirecting EF to "add in sorted position"

    In my experience there are other reasons why the application code may need different collection behavior than that which should be used during materialization. An example is the application code may need to lock an object (such as a DbContext or other object) when writing to the collection, while this locking may not be appropriate for EF materialization. Currently the only work-around for this, to my knowledge, is to have ICollection<T>.Add do something that is inconsistant with the other members of the collection and/or inconsistent with the normal definition of Add.

    Based on this experience, I suggest that some thought be given to the current design in which EF uses the same interface to add entities during materialization as is used to expose the navigation in the entity. There is an analogy with the property access mode: there is a way to cause materialization to use direct field access, which is different from the way that the property is exposed in the entity class for use in the application. It might make sense to define a separate interface, INavigationCollection<T> with an Add method. A type derived from ICollection<T> would still be used to declare the navigation property, but if the instance object also implements INavigationCollection<T> then the INavigationCollection<T> interface would be used during EF operations such as materialization. This would cleanly solve the issue of separating EF's use and application code's use of the collection.

  4. sjb-sjb commented on Mar 12, 2019

    @sjb-sjb

    A further example on the challenges of locking and EF's use of collections. If the entity design requires locking the navigation collection then, in addition to the materialization/Add conflict noted above, a conflict occurs between locked enumeration and EF's use of the collection when entities are added to the context. When an entity is added to the context, EF will access the collection enumerator in order to patch up entity relationships. However, this may need to occur when the caller already is holding the lock for other reasons. These situations can be difficult to work around because the two alternatives -- either obtain the entity lock in the enumerator, or don't -- are unsatisfactory in many cases.

    The INavigationCollection approach suggested earlier would address this example as well.

  5. rbonomo commented on Jul 10, 2019

    @rbonomo

    Please, somebody save me from ordering my child navigation properties after DB retrieval.

  6. self-assigned this
    on Oct 20, 2022
  7. AndriySvyryd commented on Oct 20, 2022

    @AndriySvyryd
    Member

    This is the navigation counterpart to #2968

  8. changed the title [-]Rich collection support: Dictionary, Lookup, HashSet, ordered collections, etc.[/-] [+]Rich collection support: Dictionary, Lookup, HashSet, etc.[/+] on Oct 21, 2022
  9. removed their assignment
    on Aug 31, 2024
  10. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions