Mando
Mando

Reputation: 11712

Cannot use SQL server (LocalDB) after moving from IIS Express to Local IIS

When I run my website under local IIS instead of embedded IIS express I got the following error when accessing database (mdf file at app_data)

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: 26 - Error Locating Server/Instance Specified)

Here the connection string:

Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Sources\Local\WebApplication1\WebApplication1\App_Data\aspnet-WebApplication1-20140226022052.mdf;Integrated Security=True

From Visual Studio and IIS Express I could connect to that database:

https://db.tt/rBXiwyA5

But the issue is that I need to have access to deployed application from another computer and IIS Express doesn't allow remote connections. That is why I was forced to use Local IIS.

Thank you for any advises!

Upvotes: 5

Views: 4221

Answers (3)

Hakan Yildizhan
Hakan Yildizhan

Reputation: 182

There are couple of things to try:

  1. Provide the connection string in the format of, e.g.
  Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-WebApplication;Integrated Security=SSPI
  1. On IIS, right click on the related App Pool then go to Advanced Settings, then set "Load User Profile" to true. In my experience this did not change anything, so I had to do this manually in the applicationHost.config file found in C:\Windows\System32\inetsrv\config. Specifically, make sure "loadUserProfile" and "setProfileEnvironment" properties are set to true:
    <applicationPools>
        ..
        <add name="MyAppPool" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated">
           <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
        </add>
        ..
    <applicationPools>

Alternatively you could try changing Identity under App Pool Advanced Settings from ApplicationPoolIdentity to LocalSystem.

  1. After doing one of these things you will hopefully stop getting this error, yet you may find that the application behaves as if there is no data in the database. This is because unlike SQLExpress or SQL Server, LocalDB runs as a user-specific process. Hence you might need to seed the database for the current database user (depending on the Application Pool/Identity being used) .

Upvotes: 0

Gustavo Rossi Muller
Gustavo Rossi Muller

Reputation: 1142

If you are using EntityFramework, try put it on App.config, worked for me.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Upvotes: 0

user3418071
user3418071

Reputation: 36

you may need to double escape the \

"Data Source=(LocalDB)\v11.0;Integrated Security=true"

Unable to connect to localDB in VS2012 – "A network-related or instance-specific error occurred while establishing a connection to SQL Server..."

Upvotes: 2

Related Questions