Repository navigation
feat(efcore)!: IsMultiTenant property, typed overload, fail-closed filter, and tenant ID validation - #1239
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four commits, each one intent, each building and passing tests on its own:
feat(efcore)!: make IsMultiTenant an entity type property:IsMultiTenanton EF Core entity type metadata is now a C# 14extensionproperty onIReadOnlyEntityType, replacing theIMutableEntityTypeandIEntityTypemethod overloads. The receiver is non-nullable, so callers passing a nullable entity type (e.g. the result ofFindEntityType) get nullable warnings.feat(efcore)!: add typed IsMultiTenant overload and fail closed without a tenant: addsIsMultiTenant<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.feat(efcore): validate the tenant id type when the model is built: a model-finalizing convention, registered byMultiTenantDbContext<TId>andMultiTenantIdentityDbContext<TId>, throws aMultiTenantExceptionwhen a multi-tenant entity'sTenantIdproperty type differs from the context's tenant ID type. New publicModelConfigurationBuilder.ValidateMultiTenantIdType<TId>()lets contexts that implementIMultiTenantDbContext<TId>directly opt in.docs: fix maintenance fee effective date typo: unrelated README typo.Behavior changes to review
InvalidOperationExceptionwrapping aNullReferenceException. 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 indocs/EFCore.md.dotnet ef migrationsandEnsureCreated. It was always broken, but it now surfaces earlier.ConfigureConventionswithout callingbase. Documented.TenantIdproperty rather than storing a model annotation, so migration snapshots are unchanged.Notes
IsMultiTenantoverloads are kept: the non-generic one builds its expression tree with reflection.ConfigureMultiTenantandMultiTenantIdentityDbContextstill use it, so switching them to the typed overload is a possible follow-up.docs/EFCore.md.docs/WhatsNew.mdis 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)🤖 Generated with Claude Code