Skip to content

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

@roji

Description

@roji

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

Metadata

Metadata

Assignees

No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions