Change the connection string dynamically (per request) on Entity Framework 7 / MVC 6

一笑奈何 提交于 2019-12-06 07:09:54

First of all, I do not recommend swapping connection strings per request.

But that's not the question. You can do this. You will need to pass your DbContext a new connection string.

.AddDbContext caches the connection string in the dependency injection container, so you cannot use DI to make this scenario work. Instead, you will need to instantiate your DbContext yourself and pass it a new connection string.

I would have preferred not to answer my own question, but I feel that I must offer guidance to those with a similar problem, after a long and deep research over internet so I can save them a lot of time testing multi-connection scenarios, wich is quite laborious...

I've finally used a (very recent) feature and APIs of Azure called "Elastic Database Tools" wich, to be concise, is a set of tools from Microsoft aimed to address this concrete problem, specially for SaaS (software as a service) scenarios (as mine is).

Here is a good link to start with:

https://azure.microsoft.com/en-us/documentation/articles/sql-database-elastic-scale-get-started/

Good luck with your projects!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!