CBreeze
CBreeze

Reputation: 2965

How to utilise multiple databases in an ASP.Net Core WebApp

I have two SQL databases, in this example one is named "Database_Core", one named "Database_Test".

When I am developing my ASP.Net app I want to target "Database_Test". After the app has been published it needs to target "Database_Core" so that the Users can access the "real" data.

Moving forward this may expand as we may have multiple clients that target their own version of a SQL database, keeping their data separated from all other clients. Currently I create a context like so;

services.AddDbContext<DashboardContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("Database_Core")));

The ConnectionString is stored in appsettings.json. In previous apps (WPF) I have had a small SQLLite file that exists on each User's PC that defines which database to target.

The issue now is that moving onto ASP.Net Core and a new web app, this method is not feasible as I simply will not have access to each User's machine.

I will be using SSDT to ensure that the Database schema is consistent across all Client Databases.

My question is, how can I maintain a Database for each client but only have one app, so that when Users enter the app their specific Database is targeted?

Upvotes: 1

Views: 1255

Answers (1)

hardkoded
hardkoded

Reputation: 21607

I wouldn't use the same Application to serve more than one site, you not only have the database but also security data and cache in the middle, and you won't want to mess that up.

What you could do is:

  • Use the same connection string key, what you are going to change is the environment.
  • Setup as many environment config files are you need.
  • Setup many WebSite/Application Pools on IIS targeting the same folder in the File System. Each site should have it's own Application pool
  • Use the Application Pool ID to load the desired environment. See https://github.com/aspnet/Docs/issues/2030

Upvotes: 1

Related Questions