Cannot connect to LocalDB
<p>I am trying to run an IIS-hosted app (in the form of a Movie Database) to teach myself how to design a 3-tier architecture program: MVC app, WCF Services, SQL Server DB. Since I don't have a license key to the full SQL Server I decided to go with LocalDB and have WCF services that wrap around my stored procedures. This is the first time I am using LocalDB and am running into issues connecting to my instance, v13.0.</p>

<p><strong>My Problem:</strong></p>

<p>I cannot, for the life of me, get my project in VS to connect to the LocalDB instance. I keep getting the following error:</p>

<blockquote>
  <p>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.</p>
</blockquote>

<p>My Connection String (from Web.config):</p>

<pre><code>&lt;connectionStrings&gt;
    &lt;add name="MovieDB" connectionString="Data Source=(localdb)\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\MovieDB.mdf" /&gt;
&lt;/connectionStrings&gt;
</code></pre>

<p>Using sqlcmd to connect to it and run queries runs fine:</p>

<p><a href="https://i.stack.imgur.com/08XBN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/08XBN.png" alt="Connecting to LocalDB instance just fine from command line"></a></p>

<p>Visual Studio SQL Server Object Explorer can connect just fine:</p>

<p><a href="https://i.stack.imgur.com/dMLGW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dMLGW.png" alt="Visual SQL Server Object Explorer can connect just fine"></a></p>

<p>The connection string, as escaped by Visual Studio when it gets to the problematic line, so I know the connection string isn't escaped incorrectly:</p>

<pre><code>"Data Source=(localdb)\\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\\MovieDB.mdf"
</code></pre>

<p>I tried following this guide (<a href="https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-1-user-profile/" rel="noreferrer">Part 1</a> and <a href="https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-2-instance-ownership/" rel="noreferrer">Part 2</a>) and the images are broken as of at least 4/18/2016, so I wasn't able to fully understand what I'm supposed to do. I was only able to get the above error because I changed the app pool identity to LocalSystem:</p>

<p><a href="https://i.stack.imgur.com/0APWk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/0APWk.png" alt="I was only able to get this error because I changed the app pool identity to LocalSystem"></a></p>

<p>When the app pool is running under the ApplicationPoolIdentity identity, I receive this error:</p>

<blockquote>
  <p>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.</p>
</blockquote>

<p>I even went so far as to modify the C:\windows\system32\inetsrv\config\applicationHost.config file to force profile loading. Still nothing.</p>

<pre><code>&lt;applicationPools&gt;
    &lt;add name="DefaultAppPool" /&gt;
    &lt;add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" /&gt;
    &lt;add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" /&gt;
    &lt;add name=".NET v2.0" managedRuntimeVersion="v2.0" /&gt;
    &lt;add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" /&gt;
    &lt;add name=".NET v4.5" managedRuntimeVersion="v4.0"&gt;
        &lt;!-- I added this, before it was an empty node --&gt;
        &lt;processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" /&gt;
    &lt;/add&gt;
    &lt;applicationPoolDefaults managedRuntimeVersion="v4.0"&gt;
        &lt;!-- This was already here --&gt;
        &lt;processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" /&gt;
    &lt;/applicationPoolDefaults&gt;
&lt;/applicationPools&gt;
</code></pre>

<p>My app is running under the ".NET 4.5" app pool</p>

<p><a href="https://i.stack.imgur.com/fGKsD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fGKsD.png" alt="My app is running under the &quot;.NET 4.5&quot; app pool"></a></p>

<p><strong>My Code:</strong></p>

<p>Page loads and calls this service to get the list of movies in the DB:</p>

<pre><code>[ServiceContract]
public interface IMovieDBService
{
    // ...

    [OperationContract]
    List&lt;Movie&gt; GetMovies(int actorID);

    // ...
}

public class MovieDBService : IMovieDBService
{
    // ...

    public List&lt;Movie&gt; GetMovies(int actorID = 0)
    {
        var output = new List&lt;Movie&gt;();
        using (var connection = DBTools.GetConnection(MovieDBConnectionName)) // Does not get past here; MovieDBConnection = "MovieDB"
        {
            var parameters = new List&lt;SqlParameter&gt;
            {
                new SqlParameter("@actorID", actorID)
            };

            using (var reader = connection.ExecuteStoredProcedure("GetMovies", parameters))
            {
                while (reader.Read())
                {
                    output.Add(new Movie
                    {
                        ID = reader.GetInt32("ID"),
                        Title = reader.GetString("Title"),
                        Year = reader.GetInt16("Year"),
                        Genre = GetGenre(reader.GetInt32("GenreID"))
                    });
                }
            }
        }

        return output;
    }

    // ...
}
</code></pre>

<p>And the problematic method from my DBTools project:</p>

<pre><code>public static SqlConnection GetConnection(string connectionName)
    {
        var connection = new SqlConnection();
        var connectionString = ConfigurationManager.ConnectionStrings[connectionName];
        if (connectionString != null)
        {
            connection.ConnectionString = connectionString.ConnectionString;
            connection.Open(); // breaks here

            return connection;
        }

        return null;
    }
</code></pre>

<p>I can only assume the problem has to do with authentication, or the fact that an IIS-hosted application is trying to connect to a LocalDB user process, but I've tried following instructions from other questions and have been unsuccessful (possibly because of failing to understand the real problem). I am at a loss...</p>