Home  /  Questions  /  Question

62   95.7
Oct 17, 2014

Changing connection string

Using Spark framework with .NET MVC 4, we have a need to change database connection based on user profile located in central database.

e.g we have 10 application database instance with similar schema definition and a user in one instance may or may not have permission to see data in other db.

It is one solution application and can not have multiple domain layers , also we can not merge all instance data and create one back-end solution. Given that server connection remains same only initial catalog name changes what would be the best way to achieve this per controller/request context  (may be using dependency injection) ? 


508   99.9
Oct 20, 2014
Hello Lauren:

This is an unusual situation. 

My initial response would be to merge those databases and add a db identifier to each record.
(I realize that this is not an option)..

My next response would be to create 10 separate domain projects, although the data model is the same for each database.
(I realize that this is not an option - although you can always move the domain code to the main project and remove the domain projects) 

The 10 domain projects will create the same objects in different namespaces for each table.
All this is strange and is unnecessary code duplication because the databases are the same.

When using only 1 domain project, I was thinking that all the Repository methods perhaps can be customized to accept a connection string (name) for each request.. This may be possible, but requires a good understanding of the underlying Core.cs and Repository.cs files (the good news is that you have all the source code). 

There is no doubt that something can be done, but at this point I am not certain what the most optimal solution is. 
It all requires some experimentation.  Hope this gets you going. 
Thank you for answering Jack! Had thought as much. Will implement and post for any issues. --- Lauren Kruglak  Oct 22, 2014
hey Lauren, where you able to modify the repository methods to pass extra connection string. Please share if you have achieved the same. thanks --- Kamal Balwani  Jun 03, 2015