Skip to content

Query: projecting naked correlated collection when tracking should return collection object of it's parent entity #14046

Closed as not planned

Description

@maumar

when we issue this query:

customers.Select(c => c.Orders)

currently we generate a new instance of Orders collection type. Instead we could/should grab the collection instance from the parent entity and use it instead. This way we can better handle all exotic cases, where customer collection is exposed as interface, doesnt have parameterless ctor etc.

In fact, we could potentially leverage include pipeline for this case. Include gets parent entity and collection of its children, associates them together and returns the parent entity. For this case the only difference is that we have to return collection instead.

Activity

  1. ajcvickers commented on Dec 3, 2018

    @ajcvickers
    Contributor

    Triage: Added a reference from the 3.0 queries issue.

  2. added this to the 3.0.0 milestone on Dec 3, 2018
  3. divega commented on Jan 25, 2019

    @divega
    Contributor

    This was discussed recently in a design meeting and the conclusion was:

    We will keep the current behavior for now unless a customer asks for this to be fixed or we need it to fix a different issue.

    This means:

    • EF Core’s change tracking never tracks collections, unless they are part of an entity
    • A query like this will always return a new, non-tracked instance of the collection type (though the elements of the collection can be tracked), if the collection type is something we know how to instantiate
    • It will throw if we don’t know how to instantiate. You can add something like ToList() as a workaround
    • We will not try to get the collection instance from the principal entity
      • neither as a strategy to instantiate the collection,
      • nor as a way to make sure we return the same collection that is already loaded when the principal entity is loaded,
      • nor as a way to obtain a collection that is fully tracked and for which additions and deletions have consequences,
      • nor as a way to upfront load a collection that then we fix up to the principal entity when it is later loaded

    The behavior could be somewhat misleading, but in several years and several O/RM products that support LINQ, we haven’t seen significant evidence that this type of behavior causes bad problems.

  4. removed this from the 3.0.0 milestone on Jan 25, 2019
  5. removed their assignment
    on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions