简体   繁体   中英

Connecting to multiple databases, dynamically (depending on the authenticated user) in an .NET 5 Web API

We currently have a CMS that our customers can use to manage the content and certain setting of the websites we make for them. We have one main database that contains the list of all customers. Each customer then has their own database, which contains all their data. We always connect to the main database first, then lookup the customer and use that info to connect to the database of the customer and get their data.

We are working on remaking our CMS (and it's API) in .NET 5. We use dependency injection for all our services and for the database connection. At the moment we have an IDatabaseConnection with the implementations MySqlDatabaseConnection and MsSqlConnection . This has some functions for executing queries, starting transactions etc. The constructor has an IOptions<MySettings> to inject the settings from the appSettings.json , which contains the connection string.

This works fine for the main database, but we are trying to find out the best way to connect to the customer's database, since the connection strings for those databases come from the main database, we don't know them beforehand.

We have been thinking about creating a factory for this, but not sure how to do that in this specific case. Another thing we found is that we implement multi tenancy, but also not sure if that is the best way. We can also just add an empty constructor to our implementations and manually create instances and set the correct connection string when we need them, but that seems to be an anti-pattern? Or is it somehow possible to inject 2 instances of our IDatabaseConnection and then set the connection string manually for one of them?

When we look for information online, we mostly come across solutions for Entity Framework or other object database mappers or by using DbContext, but as far as I know we can't use anything like that, because our CMS also has dynamic queries and things like that (I can explain more about that if you need more context).

So do you have any suggestions/recommendations for handling a use case like this? Do you need any code samples of what we have so far?

I had the same problem a few days ago.

You can change the connectionstring on the fly by this:

myDbContext.Database.GetDbConnection().ConnectionString = $@"Data Source=.\SqlExpress;Initial Catalog={dataasename};Integrated Security=True";

If you want to use custom connectionstring you can create the DbContext like this:

public static MyDbContext GetMyDbContext(string databaseName)
{
    var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

    optionsBuilder.UseSqlServer($@"Data Source=.\SqlExpress;Initial Catalog={databaseName};Integrated Security=True");

    return new MyDbContext(optionsBuilder.Options);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM