title: EF 5 Connection Strings + Simple Membership
body: <p>I am trying to clean up my web.config file as there seems to be redundancy in the connection string department. However, I can't seem to whittle it down to only being one.</p>
  
  <p>Here is what I have:
  This is a Database First scenario.</p>
  
  <p>My Context code:</p>
  
  <pre><code>public partial class InventoryMgmtContext : DbContext
  {
      public InventoryMgmtContext()
          : base("name=InventoryMgmtContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
    ...
  }
  </code></pre>
  
  <p>My connection string in web.config that corresponds to InventoryMgmtContext:</p>
  
  <pre><code>&lt;connectionStrings&gt;    
      &lt;add name="InventoryMgmtContext" connectionString="metadata=res://*/InventoryMgmt.InventoryMgmt.csdl|res://*/InventoryMgmt.InventoryMgmt.ssdl|res://*/InventoryMgmt.InventoryMgmt.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=&lt;SERVER_NAME&gt;;initial catalog=&lt;DB_NAME&gt;;persist security info=True;user id=&lt;UID&gt;;password=&lt;PWD&gt;;MultipleActiveResultSets=True;Application Name=OTIS_InvnMgmt&amp;quot;" providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>I was hoping that was all I would need, but then I saw that I also have the following connection string:</p>
  
  <pre><code>&lt;add name="DefaultConnection" connectionString="Data Source=&lt;SERVER_NAME&gt;;Initial Catalog=OTIS;Persist Security Info=True;User ID=&lt;UID&gt;;Password=&lt;PWD&gt;;MultipleActiveResultSets=True;Application Name=OTIS_InvnMgmt" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>And I have the following for the Membership and Role providers in my web.config that refer to the DefaultConnection string:</p>
  
  <pre><code>&lt;roleManager enabled="true" defaultProvider="SimpleRoleProvider"&gt;
        &lt;providers&gt;
          &lt;clear /&gt;
          &lt;add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" /&gt;
        &lt;/providers&gt;
      &lt;/roleManager&gt;
      &lt;membership defaultProvider="SimpleMembershipProvider"&gt;
        &lt;providers&gt;
          &lt;clear /&gt;
          &lt;add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" /&gt;
        &lt;/providers&gt;
      &lt;/membership&gt;
  </code></pre>
  
  <p>Finally, the initialization has the following:</p>
  
  <pre><code>public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
      {
          private static SimpleMembershipInitializer _initializer;
          private static object _initializerLock = new object();
          private static bool _isInitialized;
  
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
              // Ensure ASP.NET Simple Membership is initialized only once per app start
              LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
          }
  
          private class SimpleMembershipInitializer
          {
              public SimpleMembershipInitializer()
              {
  
                  try
                  {
                      if (!WebSecurity.Initialized)
                      {
                          WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);
                          //WebSecurity.InitializeDatabaseConnection("InventoryMgmtContext", "UserProfile", "UserId", "UserName", autoCreateTables: false);
  
                      }
                  }
                  catch (Exception ex)
                  {
                      throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                  }
              }
          }
      }
  </code></pre>
  
  <p>I changed the Role and Membership Provider to point to the InventoryMgmtContext string instead, and that works, but if I change the WebSecurity.InitializeDatabase to use InventoryMgmtContext (from the original DefaultConnection), like:</p>
  
  <pre><code>WebSecurity.InitializeDatabaseConnection("InventoryMgmtContext", "UserProfile", "UserId", "UserName", autoCreateTables: false);
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>"Unable to find the requested .Net Framework Data Provider.  It may not be installed."
  </code></pre>
  
  <p>I have searched for "DefaultConnection" throughout the project, and cannot find where or how this is being referenced.</p>
  
  <p>What am I missing?</p>