The bug that ends a SaaS business isn't a crash. It's one customer opening a page and seeing another customer's invoices.
The usual defence is a Where(x => x.OrganizationId == orgId) on every query. That works until someone writes the one query that forgets it. A code review might catch it. A test might not, because the test only has one organization in it.
When I built the multi-tenant core of ShipBlazor, I wanted forgetting to be impossible. The rule moved out of the queries and into the DbContext , which now does three things for every table that belongs to an organization:
Filters every read to the current organization.
Stamps every new row with the current organization.
Throws on any write to another organization's row.
Here's how each part works, with the tests that check it.…