Code Copied

VS 2013 error: 50 – Local Database Runtime error occurred.

问题

在Visual Studio 2013中使用Entity Framework 6 Code First开发应用程序时,自动生成DB后,可能在后来的访问中会出现下面的一段错误:

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.
)

本人的环境:Windows 7 SP1, SQL Server 2008 R2, Visual Studio。
即使重启SQL Server的服务也无法解决该问题,而且EF6生成的SQL Server文件为2012的。

原因

援引自网络上的一种说法,可能的原因是:

Windows 7 SP1 introduced a new default setting for IIS that prevents the loading of profile specific environment variables for IIS application pools. These environment variables are required for IntelliTrace to function correctly when collecting event and call information.

http://support.microsoft.com/kb/2547655

解决方法

修改applicationHost.config的IdentityType属性。

%windir%\system32\inetsrv\config\文件的applicationPools节点的内容进行修改。

修改前:

<applicationPoolDefaults>
    <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>

修改后:

<applicationPoolDefaults>
    <processModel identityType="NetworkService" loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>

引用

http://www.aspneter.com/2013/08/error-50-local-database-runtime-error-occurred/