我想将一些查询包装在SharedDbConnectionScope中,并在不同的连接字符串下执行。如何动态添加提供程序/连接字符串?
感谢
用于生成类的ActiveRecordContext.tt
和LinqTemplatesContext.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");