I'm building a multi-tenant app (single database, single schema) using ASP.NET Web Api, Entity Framework and SQL Server / Azure database.
This app will be used by 1000-5000 customers. All the tables will have a TenantId
(Guid
) column. Right now, I use single column primary key which is Id
(Guid
).
The problem with that is, I have to check if the data supplied by the user is from / for the right tenant. E.g I have a sales order table which has a CustomerId
column. Everytime user post/update sales order, I have to check if the CustomerId
is from the same tenant.
It gets worse because each tenant might have several outlets. Then I have to check TenantId and OutletId. It's really a maintenance nightmare and bad for performance.
I'm thinking to add TenantId
as primary key along with Id
. And possibly add OutletId
too. So primary key in sales order table will have multiple columns, Id
, TenantId
, OutletId
.
What is the downside of this approach? Would the performance hurt badly using composite key? Does the composite key order matter? Are there better solution for my problem?