Skip to content

Own a type by convention only if it can be embedded (Fixes #65) - #123

Merged
ajcvickers merged 2 commits into
releasefrom
issue-65-ownership
Nov 15, 2026
Merged

ajcvickers merged 2 commits into
releasefrom
issue-65-ownership

Conversation

@ajcvickers

@ajcvickers ajcvickers commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Fixes #65.

What

A type reached through a navigation was owned (embedded) by convention even when something outside its owner's document referred to it. That broke relationships and many-to-many join entities. A type is now owned by convention only if it can be embedded:

  • it is not top-level (no DbSet, explicit Entity<T>() or explicit relationship configuration);
  • nothing outside its owner's document navigates to it;
  • it does not hold a collection of an explicitly configured root, or of its would-be owner;
  • it is not a derived type of a root (it joins the root's hierarchy, in either registration order).

Explicit OwnsOne, [Owned] and Owned<T>() are never undone. Two owners embedding the same CLR type still give two separate owned types. Details and the decision are in the comment on #65.

Commits

  1. Own a type by convention only if it can be embedded: the rules above.
  2. Ownership by convention: honour [NotMapped], ...: review follow-up.
    • Collections are classified with EF's member classifier, so [NotMapped] and ignored members don't count.
    • A type that stops being embedded loses its owned-era shadow key.
    • Derived types are order-independent.
    • Model building is fast again: a 300-root chained model went from 238 s to about 3.7 s (2.1 s before the change).
    • A dead rule is removed and tests are added.

Breaking changes

Documented in BREAKING-CHANGES.md. Stored document shape changes for a type that is now a root: a sub-document becomes a document in its own collection (a derived type goes into its root's collection with a discriminator). A root type with no key property that previously built only because it had been owned now fails with "requires a primary key". An explicit OwnsOne or [Owned] keeps the old shape.

Tests

  • 15 unit tests for the demotion rules (MongoOwnedTypeDemotionTests) and 5 functional tests for the stored shape (OwnershipByConventionTests).
  • Two loose EF-X545 spec pins now assert their real failures (EF-X526, EF-X522); docs/failing-spec-tests.md counts are refreshed.
  • Each rule was mutation-checked in a scratch copy.
  • Full test-all on the rebased tip (bd3221da, on release @ 7447eaf8): EF8-EF11 in Default and NativeOnly, unit, functional and spec, 0 failures in all 24 project runs; encryption tests ran.

🤖 Generated with Claude Code

ajcvickers and others added 2 commits November 15, 2026 13:39
A type reached through a navigation was always embedded as an owned type, even
when it belongs to a root's hierarchy or when entity types outside its owner's
document reference it. That broke relationships and many-to-many join entities.

A type is now owned by convention only if it is not top-level (no DbSet, no
explicit configuration, not in a root's hierarchy) and nothing outside the
owning document references it:

- A type deriving from a root entity type is not owned.
- A type that holds a collection of its would-be owner is not owned.
- An owned type that an independent entity type navigates to, and that
  navigates back, becomes an independent entity type.
- An owned type that holds a collection of an explicitly configured entity
  type, or the join entity type of a skip navigation, becomes independent.

Explicit OwnsOne, [Owned] and Owned<T>() always win, and types embedded from
more than one place are left alone. Spec fixtures no longer need DoNotOwnTypes;
pins that now pass are removed and the failing-test inventory is updated.
Persisted document shape changes for some models (BREAKING-CHANGES.md).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…, order-independent derived types (#65)

- Classify collections with EF's member classifier, so [NotMapped] and ignored
  members no longer count as collections or back-references. Detection is
  candidate-driven and uses a walk up the base-type chain, which takes the
  300-root chained model from 238 s back to a few seconds.
- A type that stops being embedded loses the shadow key it had as an owned
  type; without a key property of its own the model now fails with
  "requires a primary key".
- A derived type joins its root's hierarchy whichever of the two is registered
  first.
- Remove the dead "explicit root registered after its owned holder" rule.
- Pin the two loose EF-X545 spec tests to their real failures, update
  BREAKING-CHANGES.md and failing-spec-tests.md, and add unit and functional
  tests for the stored document shape.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers merged commit 1299dcd into release Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Owned-by-convention for types reached through a navigation breaks entity relationships and many-to-many join entities

1 participant