Fork me on GitHub

[转载]VS2015自带的LocalDB数据库实例在IISExpress运行成功却在IIS中运行失败的解决方案

前言:

开发工具:Visual Studio 2015 Update 2.

数据库:Sql Server LocalDB V13 Express.

系统:Windows 10.

.NET Framework:4.6.1.

 

我一开始安装的是Visual Studio 2015 Update 1,后面升级到Visual Studio 2015 Update 2.

所以我的Visual Studio 2015中的Sql Server对象资源管理器窗口那里有两个连接实例:

一个是(localdb)\MSSQLLocalDB(v12.0.2000.8).

另外一个是(localdb)\ProjectsV13(v13.0.1100).

现在,我使用Entity Frameworkk 6连接(localdb)\ProjectsV13实例。

直接在Visual Studio 2015 F5调试运行(默认采用IISExpress),项目正常运行,而当我部署在IIS中,却运行失败。

总之提示连接不到数据库之类的相关错误。

我在IIS中添加了个网站,命名为:Allen.Fast.Web;创建的应用程序池,也命名为:Allen.Fast.Web.

 Allen.Fast.Web应用程序池使用集成模式运行,注意,身份标识使用的是:ApplicationPoolIdentity

 

解决方案:

Using LocalDB with Full IIS, Part 1: User Profile

http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-1-user-profile.aspx

Using LocalDB with Full IIS, Part 2: Instance Ownership

http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-2-instance-ownership.aspx

以上是解决LocalDB在IIS中的运行问题的可能的解决方案,记录一下,以备查用。

******

我最喜欢的解决方案:

1、在命令行中启用共享LocalDB连接(需要管理员权限):

sqllocaldb share ProjectsV13 ProjectsV13Share

2、使用Microsoft SQL Server Management Studio连接LocalDB测试是否成功:

服务器名称:(localdb)\.\ProjectsV13Share

身份验证:Windows身份验证

3、新建查询,为IIS应用程序池添加登录和数据库权限:

create login [IIS APPPOOL\Allen.Fast.Web] from windows;
exec sp_addsrvrolemember N'IIS APPPOOL\Allen.Fast.Web', sysadmin

(注:具体应用程序池请根据需要修改)

4、修改连接字符串的data source属性:

原:data source=(LocalDB)\ProjectsV13

改:data source=(LocalDB)\.\ProjectsV13Share;

5、OK了!

 本文后部分 转载自http://www.cnblogs.com/xwgli/p/3435282.html

posted @ 2016-05-22 10:08  VAllen  阅读(3569)  评论(0编辑  收藏  举报