Menu

Multi-tenant SaaS with EF Core: make the DbContext refuse to leak data
📰

Multi-tenant SaaS with EF Core: make the DbContext refuse to leak data

DEV Community: dotnet·Olumide Olowoyeye·about 18 hours ago
#O8OXWUhm
Long article (1399 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.