Multi-tenancy

One of our requirements is multi-tenancy support. Now I read in one of your blog posts how to implement it. But I definitely don't want a separate database for each tenant that wouldn't be maintainable with several hundred tenants. Do you have a suggestion on how to manage multi-tenancy in a single database?

Hi @Cogito,

Do you have a suggestion on how to manage multi-tenancy in a single database?

You can implement multi-tenancy as with any other ASP.NET Core application. For example by filtering the data by the current user as shown here.

partial void OnOpportunitiesRead(ref IQueryable<Opportunity> items)
{
    var userId = security.User.Id;

    // Filter the opportunities by the current user's id
    items = items.Where(item => item.UserId == userId);
}

I saw your sample which demonstrates extending the aspnetusers table to establish a relationship to the opportunities table. I have a tenant table with tenantid as pk and that should be a foreign key in aspnetusers in order to establish a tenant (1) to aspnetusers (many) relationship. Would this scenario be possible? That means each time a new user will be created the corresponding tenantid has to be stored as well. The goal is that every user in our system must be clearly assigned to a tenant.
This tenantid is also a foreign key in all other tables so I would need a global variable to store the tenantid in order to make queries on tables.

Is it possible to add a tenant table as master and link it to aspnetusers as detail?

You can add any columns to the AspNetUsers table. Here is the official documentation.

I would try to use the concept of Db Schema for tenant isolation, in SQL Server at least