title: Entity Framework 6.0 + Encrypt Connection String
body: <p>This question may be repeated but we were not able to find a good solution.</p>
  
  <p>Our project uses EF 6.0 &amp; C# and targeted for .NET 4.0 and above and finally SQL Server 2012.</p>
  
  <p>The project works fine without. But have few queries on the EF connection string and the way it stores.</p>
  
  <ol>
  <li>How to read the encrypted connection string stored in the app.config to be read in the EF's context.cs file.</li>
  <li><p>We added the following code to read encrypted connection string, it works but to an extent only i.e. till we don't add the a new stored procedure.</p>
  
  <pre><code>public MCMS_II_LogEntities() 
      // : base("name=MCMS_II_LogEntities") //Original
      :base( GetConnectionString())
      {
      }
  
  public static string GetConnectionString()
  {
      string connString = new System.Data.EntityClient.EntityConnectionStringBuilder
      {
          Metadata = "res://*",
          Provider = "System.Data.SqlClient",
          ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
          {
              InitialCatalog = "&lt;Catalog Name&gt;",
              DataSource = ConfigurationManager.AppSettings["DataSourceAddress"].ToString(),
              IntegratedSecurity = false,
              MultipleActiveResultSets = true,
              UserID = "&lt;LOGIN ID&gt;",                 // User ID such as "sa"
              Password ="&lt;PASSWORD&gt;",               // hide the password
          }.ConnectionString
      }.ConnectionString;
      return String.Format(connString);
  }
  </code></pre></li>
  <li><p>Once we add the new stored procedure or function to the EF model, the above coded added will be removed.</p></li>
  </ol>
  
  <p>Query</p>
  
  <ol>
  <li>What is the best practice to achieve this?</li>
  <li>How to address this issue.</li>
  </ol>