Search code examples
c#asp.net.netsql-serverasp.net-mvc

How to Manage Multiple Database for every Client on same Application on .NET 7


I need a suggestion. I have a .Net MVC Application that I want to give to multiple customers.

What I want to do is use the same deployed application for every customer.

I'm thinking of making multiple databases for every customer and changing the connection string each time someone logs in to the application.

Is this a good approach? Or there can be a better alternative?

And how to manage it and change between the database in my .Net 7 Application?


Solution

  • What you seem to be asking about is called multi-tenancy.

    Separate databases

    Using a separate database for every customer is one option. It's fairly safe, in that you keep one tenant's data isolated from other tenants. You can even run each database on separate servers, if warranted and desired.

    The disadvantage is that you have to set up and manage multiple databases - one for each tenant.

    This becomes particularly bothersome if you need to make schema changes after having deployed and set up multiple databases. This isn't an insurmountable obstacle, since databases tend to be quite automatable, but something you need to take into account.

    You'll also going to need to back up each database separately.

    Single database

    Another option is to use a single database, but keep track of tenants based on a TenentID column or similar.

    This means that you have to be careful to always filter any query results on that column, and be sure to always include that column when writing to the database.

    In some industries (medical, digital infrastructure, etc.) there may also be further legal ramifications where you have to convince an auditor that you have satisfactory separation of tenant data.

    The advantage to this approach, on the other hand, is that you only need to set up and operate a single database.

    Hybrid approaches

    You can combine the two by starting with the single database design. Even if you have such a design, it doesn't prevent you from creating separate copies of the database schema for select tenants. The code would still make use of the TenantID column, but all rows would have the same value.

    This is even more complicated, but can be useful in situations where you have many small tenants and a few large ones. The large tenants get their own database(s) (either because they pay extra for that, or just because they have so much data), and the small tenants share one.

    The disadvantage of this approach is the union of the above disadvantages.


    I'm sure that there are even more options than these three, so make sure to read up on multi-tenancy.