Skip to content

Auto-discover natural keys for FetchForWriting without explicit projection#4198

Merged
jeremydmiller merged 1 commit intomasterfrom
fix/4197-fetch-for-writing-natural-key
Mar 23, 2026
Merged

Auto-discover natural keys for FetchForWriting without explicit projection#4198
jeremydmiller merged 1 commit intomasterfrom
fix/4197-fetch-for-writing-natural-key

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

  • Fixes System.InvalidOperationException when using FetchForWriting() with natural keys #4197 - InvalidOperationException when using FetchForWriting() with natural keys on self-aggregating types without explicit projection registration
  • When FetchForWriting<TDoc, TId> is called with a natural key type (TId is not Guid/string), Marten now auto-discovers [NaturalKey] attributes on the aggregate type and registers an Inline snapshot projection if none exists
  • This enables the full natural key infrastructure (natural key table, inline projection, NaturalKeyFetchPlanner) to activate automatically

Test plan

  • Added Bug_4197_fetch_for_writing_natural_key with both auto-discovery and explicit registration tests
  • All 17 existing natural key tests pass
  • CI should validate full test matrix

🤖 Generated with Claude Code

…ction (#4197)

When calling FetchForWriting<TDoc, TId> with a natural key type, Marten now
auto-discovers [NaturalKey] attributes on the aggregate type and registers an
Inline snapshot projection if none was explicitly configured. This enables
FetchForWriting with natural keys on self-aggregating types without requiring
an explicit Projections.Snapshot<T>() call.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

System.InvalidOperationException when using FetchForWriting() with natural keys

1 participant