Can any ORM programmatically change its connection string at runtime?

I created my own CMS for my own use. Currently, I am already launching 3 websites using my CMS. and will continue to add.

since all websites always use the same version of my CMS, I just need to create one administration site to manage them all.

I am using Subsonic 2.x for the data access layer. on the administration website I have to put all connectionStrings

<SubSonicService defaultProvider="firstSql">
    <providers>
        <clear/>
        <add name="firstSql" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="first" generatedNamespace="firstSql"/>
        <add name="secondSql" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="second" generatedNamespace="second"/>
        <add name="thirdSql" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="third" generatedNamespace="thirdSql"/>
    </providers>
</SubSonicService>

<connectionStrings>
    <clear/>
    <add name="first" connectionString="Data Source=123.123.12.3;Initial Catalog=first;User ID=first;Password=first" providerName="System.Data.SqlClient"/>
    <add name="second" connectionString="Data Source=123.123.12.3;Initial Catalog=second;User ID=second;Password=second" providerName="System.Data.SqlClient"/>
    <add name="third" connectionString="Data Source=123.123.12.3;Initial Catalog=third;User ID=third;Password=third" providerName="System.Data.SqlClient"/>
</connectionStrings>

here is the ugly code

switch(sitename){
    case "first":
        var comment1 = new firstSql.Comment(id);
        comment1.Accepted = true;
        comment1.Save();
        break;
    case "second":
        var comment2 = new secondSql.Comment(id);
        comment2.Accepted = true;
        comment2.Save();
        break;
    case "third":
        var comment3 = new thirdSql.Comment(id);
        comment3.Accepted = true;
        comment3.Save();
        break;
}

I'm looking for a way to do it like this

/* some magic to dynamically change the connection string */
cmsSql.ConnectionString( getConnectionString(sitename) ); 

var comment = new cmsSql.Comment(id);
comment.Accepted = true;
comment.Save();

Is there any ORM solution that supports this?

Or

Do you know any workaround for this with the current ORM (Subsonic 2.x)?

UPDATE: add another example

cmsSql.ConnectionString( DB_ConString_WebsiteABC ); 

var comment = new cmsSql.Comment(id);
comment.Accepted = true;
comment.Save();

/* some magic to dynamically change the connection string */
cmsSql.ConnectionString( DB_ConString_AnotherWebsiteThatSimilarToABC ); 

var comment = new cmsSql.Comment(id);
comment.Accepted = true;
comment.Save(); // saved to another database
+3
source share
5

, LINQ-to-SQL ContextFactory, , .

static class ContextFactory
{
    public static MyDataContext CreateMyDataContent(string sitename)
    {
        var context;
        switch(sitename){
        case "first":
            context = new MyDataContext ("connection string");
        case "second":
            // and so on
        }

        return context;
    }
}

, , , : -)

+1

?
( ) ?
, " "?

( ):
Subsonic, NHibernate .

var cfg = new Configuration();

// set connectionstring programmatically
cfg.Properties["connection.connection_string"] = getConnectionString(sitename);
cfg.Configure();

cfg.AddAssembly( ... );
ISessionFactory sf = cfg.BuildSessionFactory();
0

ConnectionStrings web.config, , connectionStrings Collection ?

ConfigurationManager.ConnectionStrings[sitename].ConnectionString;


( , ) web.config, . , - -

SELECT connection FROM tblConnections WHERE siteName = 'first'

, .

0

, , - .

( CMS), - , , /// .

" , ". , , . ( , , "TENANT_ID" , where ).

NHibernate , ( ), , .

Developing which site identifier to use can be done in several ways. Our application is based on the host name, but if you do this from one site, but want to switch contexts, you can do this as a form field from the "site selector" drop-down list.

For SubSonic, I believe that you can do several databases, but the exchange between them is something that I do not know how to do.

0
source

Source: https://habr.com/ru/post/1790451/


All Articles