mardi 16 août 2022

How to do DI with DbContext that will change connection string in runtime? (C# Net 6)

in my team we have multiple databases, one for every client that works with us. In our API we only work with one database, so we do dependency injection of the DbContext using the connection string loaded inside appsettings (one instance of this API for every client). Now we have to access different databases with the same structure, and we don't know how to work properly with dependency injection in this case.

The way we have to obtain the connection string for every client is querying a parametry database that has them in a table.

enter image description here

We receive a request that contains that client_id and, based on that id, we obtain the connection string and use it to access the database.

So we must find a way to do DI after we have obtained that connection string that I need in that moment.

Aucun commentaire:

Enregistrer un commentaire