title: How to use a dynamic connection string using a Model First approach but still use the data model in the EDMX?
body: <p>I have created an EDMX using EF 5 with the Model First approach, i.e. I started with a blank designer and modeled my entities. Now I want to be able to use this model defined in the EDMX but supply runtime SQL Server connection strings without modyfing the config file.</p>
  
  <p>I know how to pass a connection string to the DbContext but the issue is locating the metadata for the mappings within the assembly.</p>
  
  <p>For example, my EDMX has this connection string in the app.config</p>
  
  <pre><code>&lt;add name="MesSystemEntities" connectionString="metadata=res://*/Data.DataContext.EntityFramework.MesSystem.csdl|res://*/Data.DataContext.EntityFramework.MesSystem.ssdl|res://*/Data.DataContext.EntityFramework.MesSystem.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=MyMachine;initial catalog=MesSystem;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>The part that I am missing is the <code>"metadata=res://*/Data.DataContext.EntityFramework.MesSystem.csdl|res://*/Data.DataContext.EntityFramework.MesSystem.ssdl|res://*/Data.DataContext.EntityFramework.MesSystem.msl;"</code></p>
  
  <p>I want to be able to create a <code>DbContext</code> programmatically passing in a SQL Server connection string but "add on" the metadata portion.</p>
  
  <p>This is what I would like to be generated by the T4 file...</p>
  
  <pre><code>public partial class MesSystemEntities : DbContext
  {
      public MesSystemEntities()
          : base("name=MesSystemEntities")
      {
      }
  
      public MesSystemEntities(string sqlServerConnectionString)
          : base(GetEfConnectionString(sqlServerConnectionString))
      {    
      }
  
      private string GetEfConnectionString(string sqlServerConnectionString)
      {
         // values added by T4 generation
         string format = "metadata=res://*/Data.DataContext.EntityFramework.MesSystem.csdl|res://*/Data.DataContext.EntityFramework.MesSystem.ssdl|res://*/Data.DataContext.EntityFramework.MesSystem.msl;;provider=System.Data.SqlClient;provider connection string=\"{0}\"";    
         return String.Format(format, sqlServerConnectionString);
      }
  ...
  }
  </code></pre>
  
  <p>My question is how can I get the metadata I need in the T4 generation file to create the Entity Framework connection without hardcoding it for each EDMX file</p>
  
  <p><em>OR</em></p>
  
  <p>is there an easier way to load the metadata from the assembly programmatically?</p>