[英]How to allow IIS to use local database from ASP.NET MVC project?
I'm going to be demoing a ASP.NET MVC website on a local network. 我将在本地网络上演示ASP.NET MVC网站。 This application has a connection to a database: 此应用程序与数据库连接:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-EBC-20141127093222.mdf;Initial Catalog=aspnet-EBC-20141127093222;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I would like if this database can be used by both IIS and whenever I run my application locally. 我想如果IIS和每当我在本地运行我的应用程序时都可以使用这个数据库。 I've made a site on IIS - it is running .NET v4. 我在IIS上创建了一个站点 - 它运行的是.NET v4。 My project lives in c:\\inetpub\\www\\ebc
. 我的项目位于c:\\inetpub\\www\\ebc
。 I can publish the website but recieve this error upon viewing the page: 我可以发布网站,但在查看页面时收到此错误:
"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. Unexpected error occurred inside a LocalDB instance API method call. See the Windows Application event log for error details." “建立与SQL Server的连接时发生与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。验证实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序: SQL网络接口,错误:50 - 发生本地数据库运行时错误。在LocalDB实例API方法调用中发生意外错误。请参阅Windows应用程序事件日志以获取错误详细信息。
I know I need to allow remote connections to the sql server through microsoft sql server manager? 我知道我需要允许通过microsoft sql server manager远程连接到sql server? Is there a way to do it elsewhere? 有没有办法在别处做?
The best solution is to use SQL Expression but if you don't want there are few steps that you have to do to make your mvc application work with localdb 最好的解决方案是使用SQL Expression但是如果你不想要的话,你需要做的几个步骤才能让你的mvc应用程序与localdb一起工作
sqllocaldb share v11.0 IIS_DB
(then the database will be shared and IIS can access it) 执行sqllocaldb share v11.0 IIS_DB
(然后数据库将被共享,IIS可以访问它) Data Source=(LocalDb)\\.\\IIS_DB
then publish again 将站点连接字符串更改为指向DB的共享实例: Data Source=(LocalDb)\\.\\IIS_DB
然后再次发布 Note that in this point you may receive an access deny error and there is how to fix it 请注意,此时您可能会收到访问拒绝错误,并且有如何修复它
For the server name enter (LocalDb)\\.\\IIS_DB
then right click on the connection and choose new query and execute this command 对于服务器名称,输入(LocalDb)\\.\\IIS_DB
然后右键单击连接并选择新查询并执行此命令
create login [IIS APPPool\\DefaultAPPPool] from windows; exec sp_addsrvrolemember N'IIS APPPool\\DefaultAPPPool, sysadmin
When there are multiple version of SQL Server localDB installed in a machine, this error is very common. 如果在计算机中安装了多个版本的SQL Server localDB,则此错误非常常见。
Try to use (localdb)\\mssqllocaldb
, as a common server name to connect to the LocalDB automatic instance, instead of "v11.0" or "v12.0". 尝试使用(localdb)\\mssqllocaldb
作为连接到LocalDB自动实例的公共服务器名称,而不是“v11.0”或“v12.0”。
The above automatic localdb instance, have a default name across all the version and avoid conflict between name & version. 上面的自动localdb实例在所有版本中都有一个默认名称,并避免名称和版本之间的冲突。
Important : When updating the localdb to new version, remember the database created with an older version does not work with the newer. 要点 :将localdb更新为新版本时,请记住使用旧版本创建的数据库不适用于较新版本。
See this post: These two posts on Using LocalDB with Full IIS should give you more information. 看到这篇文章:使用LocalDB和完整IIS的这两篇文章应该为您提供更多信息。 Especially the second part seems relevant, but the first one contains some context as well. 特别是第二部分似乎相关,但第一部分也包含一些背景。
Credit: IIS connecting to LocalDB 信用: IIS连接到LocalDB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.