title: Setting the ASP.NET Memberships DatabaseName or Connection String Dynamically in C#
body: <p>I've successfully built many applications that use the ASP.NET Membership Provider. I'm diving into a new project that is multi-tenant and I'm making scalable by partitioning user accounts across multiple SQL Server databases.</p>
  
  <p>Each database has the Memberships schema installed, and I have no problem setting any one of the databases as my Membership provider by adding the information in my Web.Config file. However, my goal is to use a master database as a look-up table. This database has a table called "Accounts" and I store the membership database for that account in a field called "MebershipPartition" that is associated with users on that account in that table (Each account can have many users).</p>
  
  <p><strong>Here is the basic architecture, with extraneous information removed:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/niBKQ.jpg" alt="enter image description here"> </p>
  
  <p>What I am trying to do is set my Membership provider to that particular provider database via an updated connection string (or even just the databaseName since this is all on the same SQL Server) <strong>AFTER</strong> I do the lookup. This means that it has to happen outside of the Web.Config and the Global.asax file.</p>
  
  <p>I also cannot pre-populate my Web.Config with a list of available connection strings because the system may generate a new Membership Database whenever certain criteria is met, such as reaching a certain cap on a previous database or of a particular account requires that it's users are isolated to an individual database. So this has to be completely dynamic. </p>
  
  <p>In my research I've tried a number of methods which are detailed in the following posts:</p>
  
  <p><a href="https://stackoverflow.com/questions/12838669/set-connection-string-of-membership-dynamically-from-code">StackOverflow</a>, <a href="http://forums.asp.net/p/997608/2209437.aspx" rel="nofollow noreferrer">ASP.NET Forums</a> and <a href="http://forums.asp.net/post/2660200.aspx" rel="nofollow noreferrer">ASP.NET Forums</a></p>
  
  <p>None of the above solutions work as desired. Either they rely on the solution to execute too early on in the application life-cycle, or they use hard coded values for the connection string and only solve the problem of removing the reliance on the Web.Config. </p>
  
  <p><strong>IDEALLY</strong> I would like to expose a single method that I can call at any time from a C# class to simply switch the database that the Membership system uses as needed, for example:</p>
  
  <pre><code>string Membership_DB_1 = "Server=[server];Database=database1;User ID=[userid];Password=[password];Trusted_Connection=False;Encrypt=True;"
  string Membership_DB_2 = "Server=[server];Database=database2;User ID=[userid];Password=[password];Trusted_Connection=False;Encrypt=True;"
  string Membership_DB_3 = "Server=[server];Database=database3;User ID=[userid];Password=[password];Trusted_Connection=False;Encrypt=True;"
  
  Membership.SwitchConnectionString(Membership_DB_1);
  Membership.SwitchConnectionString(Membership_DB_2);
  Membership.SwitchConnectionString(Membership_DB_3);
  </code></pre>
  
  <p>I'd like to thank the community in advance, I have spent many days (and now weeks) wrestling with this scenario and I am near the point of pivoting to another solution!</p>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>I've also been looking into the following solution, but again - it requires setting up the Web.Config with all potential connection strings in advance, which is not desirable from a management perspective:</p>
  
  <p><a href="http://forums.asp.net/p/1062996/3125142.aspx#3125142" rel="nofollow noreferrer">Another Hard Coded Solution</a></p>
  
  <p>Is this just not possible? Next I'll be looking into rolling my own version of the Membership Provider by using the Provider Toolkit Samples provided by Microsoft, but there aren't many samples or documentation available, and the actual downloads pages are missing in action! <strong><a href="http://weblogs.asp.net/scottgu/archive/2005/08/01/ASP.NET-2.0-Provider-Toolkit-now-available-for-Download.aspx" rel="nofollow noreferrer">Scott Guthrie posted about it</a></strong> in 2005, but as you can see the reference in the article is now missing. I ended up finding a <strong><a href="http://download.microsoft.com/download/a/b/3/ab3c284b-dc9a-473d-b7e3-33bacfcc8e98/ProviderToolkitSamples.msi" rel="nofollow noreferrer">Download Link to the MSI here</a></strong>, but I'm not sure that the code is well maintained or even still relevant... </p>
  
  <p>I was trying to avoid having to get this deep into the Provider components, but I plan to keep marching on! Will keep this thread updated as I go for any others looking to do the same!</p>