Pular para o conteúdo principal
Versão: main (dev) 🚧

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​

  1. Every table maps into the schema of its domain grouping. The schemas are constants in Infrastructure/Persistence/Schemas.cs; a configuration writes builder.ToTable("Name", Schemas.X), never a literal and never without a schema. AppDbContext.IdentitySchema and the DevPack model extensions (ApplyDevPackMfaModel, ApplyDevPackRequestSigningModel) receive the same constant.

  2. The canon's groupings:

    SchemaTables
    identityIdentity (Users, Roles, UserRoles, …), RolePermissions, PermissionActions, RefreshTokens, ServiceClients, UserAvatars, ClientCertificates, Mfa*, SignatureNonces
    tenancyTenants, TenantUsers, TenantSettings, TenantLogos
    salesOrders

    Authorization (RolePermissions, PermissionActions) stays with identity: it is keyed by Roles and has no life outside it. The opt-in examples follow the rule: Customers joins sales; OperatingWindow adds operations.

  3. dbo keeps only __EFMigrationsHistory. That is why the rule is per table and not modelBuilder.HasDefaultSchema(...): a default schema would also move the history table, and every existing database would lose track of its applied migrations.

  4. Schema names are lower-case, matching the identity schema the DevPack and ADR-006 already established.

  5. 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 in Schemas.

  6. Existing databases move with the migration MoveTablesToDomainSchemas: EnsureSchema plus ALTER SCHEMA … TRANSFER, a metadata operation — rows, indexes, constraints and foreign keys go along. Down transfers 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 (usually dbo) 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_datawriter for the API, db_ddladmin for whoever applies migrations (it creates and transfers schemas), as infra/azure grants them; a principal granted per object or ON SCHEMA::dbo must be granted on identity, tenancy and sales.
  • A scaffold that removes Orders (§ Primeira ação após dotnet new) also removes the Sales constant once nothing maps into it; the test only rejects tables outside Schemas, not unused constants.

Alternatives considered​

  • HasDefaultSchema with the history table pinned to dbo via MigrationsHistoryTable("__EFMigrationsHistory", "dbo"). Fewer ToTable arguments, but a table added without thought silently lands in the default grouping — the same failure as dbo, 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 identity already shipped; renaming it would move every Identity table for no gain.
  • Rewriting InitialCreate instead 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.