2

I have 2 databases created by a migration. When my project (Asp.net core 2.2) starts, it shows a view that contains a dropdownlist of database names. Now I want any database selected anytime to change the connection string to new database.

I can create 2 connection strings using this link. But I can`t get it to change the connection string at run time.

How can I change connection string at runtime?

Jordan Ryder
  • 2,336
  • 1
  • 24
  • 29
topcool
  • 2,498
  • 7
  • 28
  • 52
  • check this method we have incorporated in our repository to change connection string based on the tenant id https://github.com/Boriszn/DeviceManager.Api/blob/19fde1cbbad9a3b36c6b2674d5958fc1fcfcb8d3/src/DeviceManager.Api/Data/Management/ContextFactory.cs#L75 – Rudresha Parameshappa Sep 19 '19 at 18:24
  • 1
    @RudreshaParameshappa Thanks but i think your repository is incomprehensible to me – topcool Sep 19 '19 at 18:45
  • I think it depends on what method/class you're using to initiate your db connection. Some source code might help to show a couple steps of what your program is doing immediately after the connection is made. Based on the link I'm guessing you're not using Entity Framework, but what are you using? – Jordan Ryder Sep 19 '19 at 21:40
  • See also : https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core ,checking per request based on logic . – Nan Yu Sep 20 '19 at 05:31

0 Answers0