title: Entity Framework 4 Is it possible to read a connection string from another file rather than app.config?
body: <p>I am new to EF using EF4 with Database First and generation taking place.
  I have to place the connection string in a different config than the  app.config.</p>
  
  <p>How can I do it? How can I bypass it?</p>
  
  <p>I have a partial class of MyTextContext
  and I have a method like this</p>
  
  <pre><code>   public static string GenerateConnectionString()
    {
       SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
  
       // Set the properties for the data source.
       sqlBuilder.DataSource = dbServer;
       sqlBuilder.InitialCatalog = dbName;
       sqlBuilder.UserID = "YOUR_USERNAME";
       sqlBuilder.Password = "YOUR_PASSWORD";
       sqlBuilder.IntegratedSecurity = false;
  
       // Build the SqlConnection connection string.
       string providerString = sqlBuilder.ToString();
  
       // Initialize the EntityConnectionStringBuilder.
       var entityBuilder = new EntityConnectionStringBuilder();
  
       //Set the provider name.
       entityBuilder.Provider = "System.Data.SqlClient";
  
       // Set the provider-specific connection string.
       entityBuilder.ProviderConnectionString = providerString;
  
       // Set the Metadata location.
       entityBuilder.Metadata = @"res://*/myTestModel.csdl|
                              res://*/myTestModel.ssdl|
                              res://*/myTestModel.msl";
  
  
       return entityBuilder.ToString();
    }
  </code></pre>
  
  <p>I have noticed that my EFModel.designer has a constructor like these:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Initializes a new MyTestContext object using the connection string found in the 'MyTestContext' section of the application configuration file.
      /// &lt;/summary&gt;
      public MyTestContext() : base("name=MyTestContext", "MyTestContext")
      {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
      }
  
      /// &lt;summary&gt;
      /// Initialize a new MyTestContext object.
      /// &lt;/summary&gt;
      public MyTestContext(string connectionString) : base(connectionString, "MyTestContext")
      {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
      }
  
      /// &lt;summary&gt;
      /// Initialize a new MyTestContext object.
      /// &lt;/summary&gt;
      public MyTestContext(EntityConnection connection) : base(connection, "MyTestContext")
      {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
      }
  </code></pre>
  
  <p>How can I use my "GenerateConnectionString" rather then EF reading from the app.config? </p>
  
  <p>Thanks for any suggestions</p>