Click here to Skip to main content
15,920,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basic situation is:
Application manages a standard set of records for an organization. It will work on only one organization at a time.

App must be able to create a new organization on request. App should be able to create a database for a new organization using the same schema as in the original DB.

There is no need to manage or analyze data across organizations.

One organization will not know about any other organization.

The App must be able to change from one org to another on request.

So if the user wants to work with the records for an organization, they will request the organization in a manner similar to opening a new file.

The records for each org need to be in a database and I want to use EFCore.

Each set of organization records has exactly the same schema.

The record set for a single organization could become large over time.

App would ship with one Organization DB created.

Operation would be obvious until the time arrived to add a new organization.
It might go something like this:
Take in the Organization particulars (name, etc.)
Generate a DBName
Set the new connection string.
Copy the existing DB schema into the new DB
Store the new Org in the Orgs file
Reset the dbContext with the new Connection string
Operate on the new org.

Changing from one existing org to another should be simpler:
Select the desired org from the Orgs file
Reset the dbContext with the new Connection string.
Operate on the new org.

I am asking for:
Experienced advice on the feasibility of this approach.
Answers to some of the questions.
And/or references to resources for my education on approaches.

Essentially I am asking for ways to dynamically change the dbContext for a desired database.
I think the obvious questions are:
1. How is the best way to create the new DB Schema? ( in a SQL script; or a copy function that might exist I don’t know about; or…)
2. How do I reset the dbContext to the correct DB?
Thank you in advance for any advice.

What I have tried:

I have reviewed all I can find on EFCore and multiple databases. I found nothing quite like this situation.
Updated 29-May-23 14:04pm
[no name] 29-May-23 19:25pm    
Use a company code in your records / entities. Just as you'd use a country code or a client code.
Graeme_Grant 29-May-23 20:05pm    
He wants multi-tenancy. There is a tonne of information out there on the subject.
[no name] 29-May-23 22:24pm    
hardover 30-May-23 17:11pm    
I hope what I think is not what your meant. I'll just accept the comment as an eloquent, meaning, idiomatic I was not aware of.

1 solution

What you are trying to achieve is multi-tenancy. Here is a google search that will point you in the right direction: ef core multi-tenancy - Google Search[^]. First link returned: Multi-tenancy - EF Core | Microsoft Learn[^]
Share this answer
hardover 30-May-23 17:09pm    
Thank you that appears to be exactly what I needed. Great help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900