{"id":2547,"date":"2015-08-16T13:22:30","date_gmt":"2015-08-16T13:22:30","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2547"},"modified":"2015-08-16T13:22:30","modified_gmt":"2015-08-16T13:22:30","slug":"using-appsettings-in-appharbor-to-keep-connection-strings-out-of-your-git-repository","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/using-appsettings-in-appharbor-to-keep-connection-strings-out-of-your-git-repository\/","title":{"rendered":"Using AppSettings in AppHarbor to keep connection strings out of your git repository"},"content":{"rendered":"<p>When you generate a new entity framework project in C#, it generates an Application context object, which is very insistent on reading it&#8217;s connection from the connection strings settings of the web.config:<\/p>\n<pre lang=\"csharp\">\npublic class ApplicationDbContext : IdentityDbContext<ApplicationUser>\n{\n  public ApplicationDbContext()\n    : base(\"DefaultConnection\", throwIfV1Schema: false)\n  {\n  }\n\n  public static ApplicationDbContext Create()\n  {\n    return new ApplicationDbContext();\n  }\n}\n<\/pre>\n<p>Ideally you don&#8217;t want to check in the connection string. If you&#8217;re using AppHarbor, their UI helpfully lets you add AppSettings, but it takes some juggling to get these into the app context.<\/p>\n<p>You still need to have a connection string, but you can leave it blank:<\/p>\n<pre lang=\"xml\">\n<connectionStrings>\n  <add name=\"DefaultConnection\" \n      connectionString=\"\" \n      providerName=\"System.Data.SqlClient\" \/>\n<\/connectionStrings>\n<\/pre>\n<p>For testing purposes, you should also add your own AppSetting by adding this to the web.config:<\/p>\n<pre lang=\"xml\">\n<appSettings>\n    <add \n      key=\"db_connection_string\" \n      value=\"<my connection string>\"\/>\n<\/appSettings>\n<\/pre>\n<p>Then, modify the Application_Start to read the appSetting and hack it into the connection string:<\/p>\n<pre lang=\"csharp\">\nprotected void Application_Start()\n{\n  var connections =\n    ConfigurationManager.ConnectionStrings;\n\n  ConnectionStringSettings \n    defaultConnection = null;\n\n  foreach (ConnectionStringSettings c in connections) \n  {\n    if (\"DefaultConnection\".Equals(c.Name))\n    {\n      defaultConnection = c;\n    }\n  }\n\n  var fi = \n    typeof(ConfigurationElement)\n      .GetField(\"_bReadOnly\", \n          BindingFlags.Instance | \n          BindingFlags.NonPublic);\n\n      fi.SetValue(defaultConnection, false);\n\n  defaultConnection.ConnectionString = \n    ConfigurationManager.AppSettings[\"db_connection_string\"];\n}\n<\/pre>\n<p>Credit for this technique goes to <a href=\"http:\/\/david.gardiner.net.au\/2008\/09\/programmatically-setting.html\">David Gardiner<\/a> (VB example available there too if you need it for some reason).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you generate a new entity framework project in C#, it generates an Application context object, which is very insistent on reading it&#8217;s connection from the connection strings settings of the web.config: public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base(&#8220;DefaultConnection&#8221;, throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/using-appsettings-in-appharbor-to-keep-connection-strings-out-of-your-git-repository\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using AppSettings in AppHarbor to keep connection strings out of your git repository&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[96,160,169,410],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2547"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2547"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2547\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}