Menu

Multi-tenancy in EF Core: make "no tenant" mean "no rows"
πŸ“°

Multi-tenancy in EF Core: make "no tenant" mean "no rows"

DEV Community: dotnetΒ·Zohaib AhmadΒ·about 2 hours ago
#5ibpUZMX
#devtodotnet#devto#dev#tenantid#tenant
Reading 0:00
15s threshold

The worst bug a SaaS can ship is one customer seeing another customer's data. If your isolation depends on every developer remembering .Where(x => x.TenantId == tenantId) on every query, it's not a matter of if .

Here's the pattern I use in ASP.NET Core and EF Core, taken from a production point of sale and now the foundation of my SaaS Starter Kit .

1. One base class for everything a tenant owns

public interface ITenantOwned { Guid TenantId { get ; } }

public abstract class Entity { public Guid Id { get ; set ; } public DateTimeOffset CreatedAt { get ; set ; } public DateTimeOffset UpdatedAt { get ; set ; } }

public abstract class TenantEntity : Entity , ITenantOwned { public Guid TenantId { get ; set ; } }

Enter fullscreen mode

Exit fullscreen mode

2.…

Continue reading β€” create a free account

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