Shared services
Persistence and tenant-aware queries
Use module contexts, query filters and migrations without weakening isolation or transaction behavior.
Start from the owning context
Each business module owns an EF Core context. Catalog's CatalogDbContext exposes Brands, Categories and Products and uses the catalog schema. Organizations and the other modules use their own contexts. Locate the entity's owner before changing a query or migration; sharing one physical database does not make every table a public module contract.
The supplied local platform uses PostgreSQL. The persistence helpers also contain SQL Server branches, but their presence is not evidence that every current module, migration and operational path has been accepted on SQL Server. Follow the repository's delivered PostgreSQL configuration for the documented deployment.
Preserve the model conventions
BaseDbContext derives from Finbuckle's tenant-aware context. It adds named soft-delete filtering and applies tenant isolation by default to entities unless they implement IGlobalEntity. Module model builders must apply their entity configurations first and call base.OnModelCreating(modelBuilder) last.
Catalog illustrates the sequence:
modelBuilder.HasDefaultSchema(Schema);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(CatalogDbContext).Assembly);
base.OnModelCreating(modelBuilder);
This is the existing model-builder body, not a complete context class. Its ordering matters for child entities discovered through relationships. Marking an entity global is an explicit data-access decision and needs its own authorization and isolation tests.
Use the registered connection and unit of work
Register contexts through AddHeroDbContext<TContext> rather than constructing ad hoc connections in a handler. The shared persistence layer uses a scoped connection provider so contexts using the same connection string in one DI scope can share a connection and transaction.
A tenant-specific connection string overrides the shared connection through the same provider. This is important when a business write and an outbox message must commit together. Two sequential SaveChangesAsync calls do not automatically establish a transaction spanning both contexts; use the existing transaction infrastructure and verify rollback behavior.
For read paths, project only the fields needed by the response and use AsNoTracking when no mutation follows. Keep paging in the database. If a query bypasses filters for a trash or operator view, review exactly which filter is disabled and explicitly preserve tenant and permission restrictions.
Change the schema through the migrator
Schema changes belong in the migrations project and are applied by GhaylStack.DbMigrator. A module registers an IDbInitializer; Catalog's initializer applies pending migrations and deliberately has no automatic per-tenant sample data. Demonstration catalog data belongs to seed-demo.
Follow database migrations for generation and application commands. Review the SQL impact on an existing database, especially required columns, uniqueness and deletions. A fresh development database cannot reveal every upgrade problem.
Verify the behavior that matters
Test a record in two tenants, a normal list versus a trash list, and both shared and dedicated connections if your change uses them. For a transaction with an event, cause a controlled failure before commit and confirm neither the business row nor its outbox message persists. For a read change, check ordering and pagination with enough records to span multiple pages.
Investigate missing tables in migrator output, missing records in tenant context and filters, and concurrent write failures in entity constraints. See eventing for the durable-delivery side of persistence.