dotnet / dotnet/efcore

Cosmos: Implement proper entity equality and contains logic for owned (and complex) types

Open
#34,027 5 comments 3 reactions 0 assignees View on GitHub
area-cosmos area-query breaking-change customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

When comparing a top-level, non-owned entity type to another, the EF behavior is to translate that to key comparison - we check whether the two instances are the same ("referentially identical"). However, for complex types and owned types with synthesized keys (e.g. within a JSON document), this translation is not useful - these types have no (useful) keys, and since they can only be contained by a single owner, referential comparison will always yield false (unless comparing the owned entity to itself).

As a result, we think it makes sense to perform value comparison (this is self-evident in the complex type case, but less so in the owned entity case). In the Cosmos (and general JSON) context, the most natural translation is to simply perform JSON object equality in the database. That means that `Where(b => b.ShippingAddress == b.BillingAddress)` should simply translate to `WHERE b["ShippingAddress"] = b["BillingAddress"]`, allowing the database to compare the two JSON objects.

Note that we allow users to have unmapped (unmodeled) content in JSON documents in the database. If such content exists, then the JSON object comparison may fail, as one side of the comparison may have some extra properties. This can be worked around by the user breaking down the comparison to a memberwise comparison, explicitly indicating which properties should be compared.

Note that this includes the Contains implementation as well as simple equality.

/cc @ajcvickers @AndriySvyryd

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.