问题
I want to wrap some queries in a SharedDbConnectionScope and execute them under a different connection string. How do I add a provider/connection string dynamically in order to do this?
Thanks
回答1:
Both the ActiveRecord\Context.tt
and the LinqTemplates\Context.tt
that you would use to generate your classes contain constructors:
public <#=DatabaseName#>DB(string connectionStringName)
{
DataProvider = ProviderFactory.GetProvider(connectionStringName);
Init();
}
public <#=DatabaseName#>DB(string connectionString, string providerName)
{
DataProvider = ProviderFactory.GetProvider(connectionString,providerName);
Init();
}
So you can pass your connection string to one of these constructors, like:
// point to a certain connection string in the app.config
var db = new MySample("SomeConnectionStringName");
// Use a specific connection string, not the app.config
var db = new MySampleDB(@"server=.\SQL2008;database=Sample;integrated security=true;", "System.Data.SqlClient");
来源:https://stackoverflow.com/questions/9881626/how-do-you-change-subsonic-3s-connection-string-on-the-fly