简体   繁体   English

如何允许IIS从ASP.NET MVC项目中使用本地数据库?

[英]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一起工作

  1. Open Visual studio command prompt as administrator 以管理员身份打开Visual Studio命令提示符
  2. execute sqllocaldb share v11.0 IIS_DB (then the database will be shared and IIS can access it) 执行sqllocaldb share v11.0 IIS_DB (然后数据库将被共享,IIS可以访问它)
  3. Change the site connection string to point to the shared instance of DB: 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 请注意,此时您可能会收到访问拒绝错误,并且有如何修复它

  1. Go to visual studio => databaseexplorer => add connection 转到visual studio => databaseexplorer =>添加连接
  2. 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. 特别是第二部分似乎相关,但第一部分也包含一些背景。

  1. Part 1: User Profile 第1部分:用户配置文件
  2. Part 2: Instance Ownership 第2部分:实例所有权

Credit: IIS connecting to LocalDB 信用: IIS连接到LocalDB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从IIS 7.5上托管的ASP.NET MVC项目连接到mysql数据库 - Unable to connect to mysql database from ASP.NET MVC project hosted on IIS 7.5 从IIS上存储的文件中恢复ASP.NET MVC项目 - Recover ASP.NET MVC Project From Files Stored On IIS IIS 虚拟目录,并从 ASP.NET MVC 项目访问其内容 - IIS VirtualDirectories, and accessing their contents from a ASP.NET MVC Project 如何在IIS中配置asp.net MVC项目? - How to configure asp.net MVC project in IIS? 如何在没有 IIS 的情况下在 pc 上运行 ASP.NET MVC 项目? - How to run ASP.NET MVC project without IIS on pc? 为什么 IIS 服务器返回 ASP.NET CORE 5 MVC 项目的 null baseUrl,但在本地服务器上返回 true? - Why IIS Server returns null baseUrl of ASP.NET CORE 5 MVC project, but returns true on local server? 如何在另一个asp.net mvc项目中使用asp.net mvc项目功能 - How to use an asp.net mvc project capabilities in a different asp.net mvc project 如何使用 ASP.NET MVC 项目参考从控制台应用程序使用 ASP.NET MVC 的 MYSQL DbContext - How to use MYSQL DbContext of ASP.NET MVC from console application using ASP.NET MVC project reference 如何在ASP.NET MVC4中使用本地驱动器路径 - How to use local drive path in ASP.NET MVC4 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM