The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Switching Connections at Runtime

Last post 08-25-2007, 6:30 AM by David.Parsons. 0 replies.
Sort Posts: Previous Next
  •  08-25-2007, 6:30 AM 4769

    Switching Connections at Runtime

    This particular example demonstrates how to switch between development and production versions of a database at runtime. In his case the databases have the same schema, so we only need to run the Generated Master against one of them.

    But, this technique has many uses. Since there are no overlapping table names in Northwind and pubs, you could run Generated Masters for both. You would switch the default connection as needed, and access both of them from the same app. Even if there were a table name conflict in the two databases, you could alias one of them in MyGeneration, so that it generated with a different class name. The databases do not need to be on the same server. In fact, they do not even have to use the same provider.

    If you want to access the same database from two different providers, say Northwind on SQL Server and Access, see the Multi-provider FAQ for details on how to generate MetadataMaps and their use.

    When you Generate, check Ignore Catalog and Schema. By default, the database (Catalog) is hard coded in the Metadata class. To switch connections based on the connection string, be sure to check these.

    In the App/Web config file, remove all EntitySpaces configuration elements, including the "EntitySpaces" configSections entry, and the entire < EntitySpaces > sectionGroup.

    Add standard config file connection strings as described in MSDN.

    Code:
    < connectionStrings>
        < add name="DevelopmentDb"
          connectionString=
             "Data Source=WB-DNP;Initial Catalog=NorthwindDev;...">
        < add name="ProductionDb"
          connectionString=
             "Data Source=WB-DNP;Initial Catalog=Northwind;...">
    </connectionStrings>

    See the Configless section of the Getting Started PDF, and add both your EntitySpaces configurations. Since they are one time operations, the best place to put them is where you register the loader.

    Code:
    using EntitySpaces.Interfaces;
    
    // Add a connection	
    esConnectionElement conn = new esConnectionElement();
    
    conn.Name = "SqlDev";
    conn.ConnectionString = "AppSettings:DevelopmentDb";
    conn.Provider = "EntitySpaces.SqlClientProvider";
    conn.ProviderClass = "DataProvider";
    conn.SqlAccessType = esSqlAccessType.DynamicSQL;
    conn.ProviderMetadataKey = "esDefault";
    conn.DatabaseVersion = "2005";
    
    esConfigSettings.ConnectionInfo.Connections.Add(conn);
    
    // Add another connection
    conn = new esConnectionElement();
    
    conn.Name = "SqlProd";
    conn.ConnectionString = "AppSettings:ProductionDb";
    conn.Provider = "EntitySpaces.SqlClientProvider";
    conn.ProviderClass = "DataProvider";
    conn.SqlAccessType = esSqlAccessType.DynamicSQL;
    conn.ProviderMetadataKey = "esDefault";
    conn.DatabaseVersion = "2005";
    
    esConfigSettings.ConnectionInfo.Connections.Add(conn);
    
    // Assign the default connection	
    esConfigSettings.ConnectionInfo.Default = "SqlDev";
    
    // --- Register the Loader ---
    EntitySpaces.Interfaces.esProviderFactory.Factory =
        new EntitySpaces.LoaderMT.esDataProviderFactory();

    Notice above, that instead of hard coding the two connection strings, we use the "AppSettings:" prefix and reference our standard connection string names from the config file. This feature was introduced in Release 2007.0.0521.2.

    Now, you can change the default connection at runtime. The new default connection will be used for all entities and collections until you change it again. You can even load a collection from one connection, switch, and save to the other:

    Code:
    private void buttonSwitch_Click(object sender, EventArgs e)
    {
        if (esConfigSettings.ConnectionInfo.Default == "SqlProd")
        {
            esConfigSettings.ConnectionInfo.Default = "SqlDev";
        }
        else
        {
            esConfigSettings.ConnectionInfo.Default = "SqlProd";
        }
    }

    You can still change the connection per instance, again loading from one database and saving to another, but it is not necessary.

    Code:
    Employees emp = new Employees();
    emp.es.Connection.Name = "SqlProd";
    

    EDIT 2007-11-28: Corrected information on when default connection may be changed.


    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML