Skip to content

feat(efcore)!: IsMultiTenant property, typed overload, fail-closed filter, and tenant ID validation - #1239

Merged
AndrewTriesToCode merged 4 commits into
mainfrom
feat/efcore-is-multitenant
Nov 18, 2026
Merged

AndrewTriesToCode merged 4 commits into
mainfrom
feat/efcore-is-multitenant

Conversation

@AndrewTriesToCode

Copy link
Copy Markdown
Contributor

Summary

Four commits, each one intent, each building and passing tests on its own:

  1. feat(efcore)!: make IsMultiTenant an entity type property: IsMultiTenant on EF Core entity type metadata is now a C# 14 extension property on IReadOnlyEntityType, replacing the IMutableEntityType and IEntityType method overloads. The receiver is non-nullable, so callers passing a nullable entity type (e.g. the result of FindEntityType) get nullable warnings.
  2. feat(efcore)!: add typed IsMultiTenant overload and fail closed without a tenant: adds IsMultiTenant<TEntity, TId>(EntityTypeBuilder<TEntity>), whose filter is a compile-time expression (no reflection, no trim/AOT warnings of its own). The tenant query filter of both overloads now returns no rows when the context has no tenant set.
  3. feat(efcore): validate the tenant id type when the model is built: a model-finalizing convention, registered by MultiTenantDbContext<TId> and MultiTenantIdentityDbContext<TId>, throws a MultiTenantException when a multi-tenant entity's TenantId property type differs from the context's tenant ID type. New public ModelConfigurationBuilder.ValidateMultiTenantIdType<TId>() lets contexts that implement IMultiTenantDbContext<TId> directly opt in.
  4. docs: fix maintenance fee effective date typo: unrelated README typo.

Behavior changes to review

  • Null tenant now returns no rows. Previously a query through a context with no tenant threw an InvalidOperationException wrapping a NullReferenceException. This fails closed, but a tenant that never gets bound (e.g. missing middleware) now shows up as missing data rather than an exception. Documented in docs/EFCore.md.
  • A mismatched tenant ID type now throws when the model is built, not on the first query. That includes dotnet ef migrations and EnsureCreated. It was always broken, but it now surfaces earlier.
  • The check is skipped if a derived context overrides ConfigureConventions without calling base. Documented.
  • The validation reads the entity's TenantId property rather than storing a model annotation, so migration snapshots are unchanged.

Notes

  • Both IsMultiTenant overloads are kept: the non-generic one builds its expression tree with reflection. ConfigureMultiTenant and MultiTenantIdentityDbContext still use it, so switching them to the typed overload is a possible follow-up.
  • Docs updated in docs/EFCore.md. docs/WhatsNew.md is left to release automation.

Test plan

  • dotnet test test/Finbuckle.MultiTenant.Test (331)
  • dotnet test test/Finbuckle.MultiTenant.AspNetCore.Test (198)
  • dotnet test test/Finbuckle.MultiTenant.EntityFrameworkCore.Test (159)
  • dotnet test test/Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test (91)
  • Tenant isolation tests run on a shared cached model for both overloads: A/B/A/B reuse, interleaved live contexts, no tenant, base and derived entity types.

🤖 Generated with Claude Code

AndrewTriesToCode and others added 4 commits November 17, 2026 23:14
IsMultiTenant on EF Core entity type metadata is now a C# 14 extension
property on IReadOnlyEntityType instead of separate IsMultiTenant()
methods on IMutableEntityType and IEntityType.

BREAKING CHANGE: `IsMultiTenant` on entity types was a method and is now a
property. Its receiver is now non-nullable, so callers that pass a nullable
entity type (for example the result of `FindEntityType`) get nullable
warnings and must check for null first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ut a tenant

Add IsMultiTenant<TEntity, TId> on EntityTypeBuilder<TEntity>. It builds
the query filter from a compile-time expression instead of with
reflection, so it adds no trimming or AOT warnings of its own.

The tenant query filter of both overloads now returns no rows when the
context has no tenant set.

BREAKING CHANGE: Querying a multi-tenant entity type through a context
without a tenant set now returns no rows. It previously threw an
InvalidOperationException ("An exception was thrown while attempting to
evaluate a LINQ query parameter expression") wrapping a
NullReferenceException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A model finalizing convention registered by MultiTenantDbContext and
MultiTenantIdentityDbContext checks each multi-tenant entity type and
throws a MultiTenantException if its TenantId property type differs from
the context's tenant id type. Previously a mismatch surfaced as an
InvalidOperationException wrapping a NullReferenceException when the
first query ran. Entity types that are not multi-tenant, including those
marked IsNotMultiTenant, are not checked.

The check reads the TenantId property instead of recording the tenant id
type as a model annotation, so migration snapshots are unchanged.

Add ModelConfigurationBuilder.ValidateMultiTenantIdType<TId> so contexts
that implement IMultiTenantDbContext directly can opt in. A derived
context that overrides ConfigureConventions must call the base
implementation to keep the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AndrewTriesToCode
AndrewTriesToCode merged commit bbe5519 into main Nov 18, 2026
5 checks passed
@AndrewTriesToCode
AndrewTriesToCode deleted the feat/efcore-is-multitenant branch November 18, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant