开发者

Dynamically connect to different databases in a hybrid multitenant solution

I was researching about multi-tenancy and multi-instance approaches and chose a hybrid.

I keep a single instance of my ASP.Net MVC 2 application but clone the database structure for each client. I am using LINQ to SQL.

Right now, I have one instance of both so just a single connection string is wor开发者_Python百科king.

I am planning to expand it for many clients.

I can write code to create a clone of the database structure, create a db user and give permissions whenever a new client signs up. I also will save all these details (db name, db user, allowed app users) in a table.

But how can I make the app use a different database based on who is logged in?

I can't even figure out the approach I should take to do this. Should I programmatically add connection string to web.config? Is there another way to do this?


One of the constructors for DataContext accepts the connection string. You can assemble this string based on the user for the specific request.

This will override the default connection.


Check the constructor overloads for the DataContext, there is one that takes connection string. You just need to map a string property against the logged in user.

Please don't programmatically add EVERY user's connection string to web.config, think scalability!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜