سؤال

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

هل كانت مفيدة؟

المحلول

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");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top