How do you change SubSonic 3's connection stri

2019-05-30 16:27发布

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条回答
\"骚年 ilove
2楼-- · 2019-05-30 17:10

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");
查看更多
登录 后发表回答