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.
My Problem:
I cannot, for the life of me, get my project in VS to connect to the LocalDB instance. I keep getting the following error:
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.
My Connection String (from Web.config):
<connectionStrings>
<add name="MovieDB" connectionString="Data Source=(localdb)\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\MovieDB.mdf" />
</connectionStrings>
Using sqlcmd to connect to it and run queries runs fine:
Visual Studio SQL Server Object Explorer can connect just fine:
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:
"Data Source=(localdb)\\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\\MovieDB.mdf"
I tried following this guide (Part 1 and Part 2) 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:
When the app pool is running under the ApplicationPoolIdentity identity, I receive this error:
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.
I even went so far as to modify the C:\windows\system32\inetsrv\config\applicationHost.config file to force profile loading. Still nothing.
<applicationPools>
<add name="DefaultAppPool" />
<add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0" managedRuntimeVersion="v2.0" />
<add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
<add name=".NET v4.5" managedRuntimeVersion="v4.0">
<!-- I added this, before it was an empty node -->
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>
<applicationPoolDefaults managedRuntimeVersion="v4.0">
<!-- This was already here -->
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>
</applicationPools>
My app is running under the ".NET 4.5" app pool
My Code:
Page loads and calls this service to get the list of movies in the DB:
[ServiceContract]
public interface IMovieDBService
{
// ...
[OperationContract]
List<Movie> GetMovies(int actorID);
// ...
}
public class MovieDBService : IMovieDBService
{
// ...
public List<Movie> GetMovies(int actorID = 0)
{
var output = new List<Movie>();
using (var connection = DBTools.GetConnection(MovieDBConnectionName)) // Does not get past here; MovieDBConnection = "MovieDB"
{
var parameters = new List<SqlParameter>
{
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;
}
// ...
}
And the problematic method from my DBTools project:
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;
}
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...
Try this: In your applicationhost file try to change processModel identityType to SpecificUser and also include username and password. OR change your app pool identity to the user that has permission to the database. When you run as the application pool identity, based on your screenshots you are running as local system - which is a predefined local account whereas sqlcmd is running as the user you are logged in as.