Sql Server 2012 Local DB发布到服务器端后无法访问

时间:2023-03-08 15:04:42
Sql Server 2012 Local DB发布到服务器端后无法访问

背景

基于Windows认证的Web application, 通过Visual Studio 2013创建的LocalDB位于App_Data目录下

现象

本地调试没有任何问题。发布到服务器(Windows Server 2008R2, IIS7.5)后,在浏览器访问抛出异常 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 create an automatic instance. See the Windows Application event log for error details.

原因

LocalDB基于用户模式运行,而IIS默认使用服务帐户,因此失去了对localDB的访问权。

解决办法

在IIS中打开对应的Web应用池属性,设置Identity为特定的域用户,并将Load User Profile的值设为True,程序恢复正常运行