How to split a connection string between multiple data objects

I have a project that has 4 entity data models. To build them, I do not want to save the connection string in my project, and I want to save the connection string in files app.configand share it between my models. How can i do this?

thank

+3
source share
3 answers

Assume first that DbContext and model / database.

  • Leave the generated EF connection strings in the app.config file intact. As Arthur said, they contain paths to EF metadata (csdl / ssdl / msl). They are also used during development by a fashion designer.
  • , , "SharedConnection". , .
  • , DbContext .
  • EF- . , :
public class BaseContext : DbContext
{
    public BaseContext(string nameOrConnectionString)
        : base(CreateConnection(nameOrConnectionString), true)
    {
    }

    private static EntityConnection CreateConnection(string connectionString)
    {
        // Create a (plain old database) connection using the shared connection string.
        DbConnection dbConn = Database.DefaultConnectionFactory.CreateConnection(
            ConfigurationManager.ConnectionStrings["SharedConnection"].ConnectionString);

        // Create a helper EntityConnection object to build a MetadataWorkspace out of the
        // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
        EntityConnection wsBuilder = new EntityConnection(connectionString);

        // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
        return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
    }
}

, , BaseContext. CreateConnection. .

private static EntityConnection CreateConnection(string connectionString)
{
    // Find the name of the shared connection string.
    const string appSettingKey = "SharedConnectionStringName";

    string sharedConnectionStringName = ConfigurationManager.AppSettings[appSettingKey];
    if (string.IsNullOrEmpty(sharedConnectionStringName))
    {
        throw new Exception(string.Format(
            "Shared connection not configured. " +
            "Please add a setting called \"{0}\" to the \"appSettings\" " +
            "section of the configuration file.", appSettingKey));
    }

    // Create a (plain old database) connection using the shared connection string.
    ConnectionStringSettings backendSettings =
        ConfigurationManager.ConnectionStrings[sharedConnectionStringName];
    if (backendSettings == null)
    {
        throw new Exception(string.Format(
            "Invalid connection string name \"{0}\" in appSetting \"{1}\"",
            sharedConnectionStringName, appSettingKey));
    }

    System.Data.Common.DbConnection dbConn =
        Database.DefaultConnectionFactory.CreateConnection(
        backendSettings.ConnectionString);

    // Create a helper EntityConnection object to build a MetadataWorkspace out of the
    // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
    EntityConnection wsBuilder = new EntityConnection(connectionString);

    // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
    return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
}
+2

Code First. , - "name=MyConnectionStringName" DbContext.

First EDMX EF :

  • " ", ,
  • EF, .

, .

, :  - -  -  -  - , MetadataWorkspace EntityConnection  - EntityConnection ObjectContext  - DbContext, ObjectContext DbContext

, , app.config.

+1

You can pass an EF connection string to an ObjectContext. If you have several models, you can put each connection string in your app.config, give each key and see when it is needed, and pass the corresponding string to the context when you initiate it.

0
source

All Articles