I have a scenario in which my application connects to different databases with respect to subdomains in url.
I have tried this code:
public class Provider : SqlMembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// Update the private connection string field in the base class.
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder["Data Source"] = "My-PC\\SQLEXPRESS";
string dbName;
if (!string.IsNullOrEmpty(Helpers.RouteManager.GetSubDomain()))
{
dbName = Helpers.RouteManager.GetSubDomain().ToString()+"_db";
}
else
{
dbName = "dbName";
}
bool isEqual = string.Equals(name, dbName);
sqlBuilder["Initial Catalog"] = dbName;
sqlBuilder["Integrated Security"] = true;
sqlBuilder["Application Name"] = "EntityFramework";
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, sqlBuilder.ConnectionString);
}
}
And I am calling this function from Application_BeginRequest()
method.
The issue I am facing is that when I build application and it hits url with specific sub-domain, it connects to the specified database but I try different sub-domain it sticks with the same database for asp.net membership.
But when i rebuild the application and try another sub domain it works.
It seems that my code is working only for the first time after I build the solution.
Please someone guide me.
Thanx