What is multi tenancy?

When you're making an application that's going to be used by groups of users, it can be useful to bake in the concept of tenancy, to simplify the way in which the application is written. Through use of tenancy, you can enforce ownership of resources at a data level, and simplify access control via low level changes to your ORM such as EF Core.

This means that application code can be written such that the tenant can be "ignored", and the repetitive checks for ownership are implicit. The application can be mostly ignorant of the tenant, and instead focus on the business logic. This enables you to store all of your users data in the same database, while still enforing strict low level controls on data availability - users from different tenants will not be able to see each others data, and your data access code doesn't have to worry about filtering results or assigning ownership to a certain tenant.

This is a simplistic but effective way of modeling ownership, and it's a nice simple way of building strict group based access control in to your ORM. You could layer permissions on top of tenancy to further control access if necessary.

What are the alternatives for enforcing ownership?

There are many alternatives for enforcing ownership of resources and access to them. I have been playing with the fantastic ORY stack recently, and they have an open source implementation of the Google's Zanzibar system which I'll cover in future, it uses ACLs for modelling access to resources. There's also the fantastic Casbin which is a multi paradigm authorization library.

How can you accomplish this with efcore?

Firstly, it's important that your system Users have the concept of Tenancy, and a TenantId attached to them. This can be easily injected as an extra claim with your choice of authentication system.

Whatever authentication system you use, you need to create a tenancy provider that EF Core can consume. The tenancy provider allows EF Core to get the tenant of the current caller, and subsiquently filter results or inject ownership for any resources.

Summary