我想在SharedDbConnectionScope中包装一些查询,并在不同的连接字符串下执行它们。如何动态添加提供者/连接字符串才能执行此操作?
由于
答案 0 :(得分:2)
用于生成类的ActiveRecord\Context.tt
和LinqTemplates\Context.tt
都包含构造函数:
public <#=DatabaseName#>DB(string connectionStringName)
{
DataProvider = ProviderFactory.GetProvider(connectionStringName);
Init();
}
public <#=DatabaseName#>DB(string connectionString, string providerName)
{
DataProvider = ProviderFactory.GetProvider(connectionString,providerName);
Init();
}
因此,您可以将连接字符串传递给其中一个构造函数,例如:
// 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");