Set search_path dynamically for schema-based multi tenancy in ASP.NET Core

asp.net-core c# entity-framework-core multi-tenant npgsql

Question

I'm writing a new ASP.NET Core application (.NET Core 3.1) using EF Core for database access. This application should interface with an existing PostgreSQL database that has a PostgreSQL schema for each tenant. From what I read so far, this is not the way one would usually do this in .NET Core, but as the database already exists I don't have any choice here. I couldn't find any good information on how to dynamically switch schemas, the documentation for multi-tenant database access generally assumes different models than tenant-per-schema or different databases.

The table layout is identical in all schemas, so the only thing that has to happen is that the search_path is set to the correct schema for each request. I'm not sure how to do this exactly with EF Core, the documentation I found only addresses setting the schema statically or per model.

What is the correct way to handle this kind of multi-tenant setup in ASP.NET Core using EF Core?

1
0
4/3/2020 9:27:21 AM

Popular Answer

Personally, I would implement some sort of DbContextProvider that would take in the name of the tenant and return a DbContext. The implementation of this DbContextProvider would look something like the accepted answer to this question.

0
4/3/2020 9:39:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow