ADR-020 — Every table in the schema of its domain grouping; dbo holds only __EFMigrationsHistory
Date: 2026-10-13
Status: Accepted
Restores: the practice of the first template generations and of ITLab.WorkGuardian (Data/EntityFrameworkCore/Schemas.cs), where no table lived in dbo.
Relates to: ADR-006 (Identity tables in the identity schema).
Context
Template.Next kept the practice only half-way. ADR-006 put the Identity tables in identity, and later
additions followed it case by case (refresh tokens, service clients, avatars, client certificates, MFA, signature
nonces). Everything else was mapped with a bare ToTable("Name") and landed in dbo: Orders, Tenants,
TenantUsers, TenantSettings, TenantLogos, PermissionActions and RolePermissions. Nothing said which
was right, so each new configuration copied whichever neighbour it was written next to — the canon had two
answers to "where does this table go".
The schema is the cheapest boundary SQL Server offers between domain groupings: it shows up in every query plan,
script and object explorer, and permissions can be granted per schema. A database where half the tables sit in
dbo loses that boundary for exactly the half nobody thought about.
Decision
-
Every table maps into the schema of its domain grouping. The schemas are constants in
Infrastructure/Persistence/Schemas.cs; a configuration writesbuilder.ToTable("Name", Schemas.X), never a literal and never without a schema.AppDbContext.IdentitySchemaand the DevPack model extensions (ApplyDevPackMfaModel,ApplyDevPackRequestSigningModel) receive the same constant. -
The canon's groupings:
Schema Tables identityIdentity ( Users,Roles,UserRoles, …),RolePermissions,PermissionActions,RefreshTokens,ServiceClients,UserAvatars,ClientCertificates,Mfa*,SignatureNoncestenancyTenants,TenantUsers,TenantSettings,TenantLogossalesOrdersAuthorization (
RolePermissions,PermissionActions) stays withidentity: it is keyed byRolesand has no life outside it. The opt-in examples follow the rule:Customersjoinssales;OperatingWindowaddsoperations. -
dbokeeps only__EFMigrationsHistory. That is why the rule is per table and notmodelBuilder.HasDefaultSchema(...): a default schema would also move the history table, and every existing database would lose track of its applied migrations. -
Schema names are lower-case, matching the
identityschema the DevPack and ADR-006 already established. -
Guarded by a test, not by review.
EveryTableHasDomainSchemaTests(Infrastructure.Tests) reads the SQL Server model without opening a connection and fails when a table has no schema or uses one that is not a constant inSchemas. -
Existing databases move with the migration
MoveTablesToDomainSchemas:EnsureSchemaplusALTER SCHEMA … TRANSFER, a metadata operation — rows, indexes, constraints and foreign keys go along.Downtransfers them back to the default schema.
Consequences
- A new aggregate joins the schema of its grouping; a new grouping is a new constant in
Schemas— and a conscious decision, since the test will not let a table in without one. - Raw SQL, reporting views and scripts outside the application must qualify the moved tables
(
tenancy.Tenants,sales.Orders,identity.RolePermissions). Unqualified names resolve against the caller's default schema (usuallydbo) and stop finding them after the migration. - The application's database principal needs rights on the new schemas. Database-wide roles are unaffected —
db_datareader/db_datawriterfor the API,db_ddladminfor whoever applies migrations (it creates and transfers schemas), asinfra/azuregrants them; a principal granted per object orON SCHEMA::dbomust be granted onidentity,tenancyandsales. - A scaffold that removes
Orders(§ Primeira ação apósdotnet new) also removes theSalesconstant once nothing maps into it; the test only rejects tables outsideSchemas, not unused constants.
Alternatives considered
HasDefaultSchemawith the history table pinned todboviaMigrationsHistoryTable("__EFMigrationsHistory", "dbo"). FewerToTablearguments, but a table added without thought silently lands in the default grouping — the same failure asdbo, under another name. Rejected: the point is that each table's grouping is a decision.- PascalCase schemas, as in WorkGuardian. Rejected for consistency with the lower-case
identityalready shipped; renaming it would move every Identity table for no gain. - Rewriting
InitialCreateinstead of adding a migration. Rejected: released versions of the template are in use, and their databases need the transfer. - Promoting the guard to the DevPack (a model assertion like
AssertEveryMultiTenantEntityIsFiltered). The rule is universal enough to qualify under G-01; kept in the template until a second consumer needs it.