使用 LocalDB 发布 ASP.net 个网站

Publishing ASP.net website With LocalDB

我正在尝试在家庭网络中的另一台计算机上发布我的网站。我已经在那台计算机上设置了 IIS 和 SQL Server 2016 Express。我的开发笔记本电脑上的版本相同。当我 运行 该网站时,出现以下错误。我认为问题可能是我正在尝试使用 LocalDB。我经常在没有互联网连接的地区旅行。这就是为什么我认为最好在开发时使用 localDB。回到家后,我会发布到我的服务器。

最初,LocalDB 是在我的计算机上创建的,位于远离我的项目的文件夹中。所以我所做的就是将它移到 app_data 以便在我发布时附加它。

我尝试了什么:

  1. 重新安装了 Microsoft SQL Server 2016(确保安装了 LocalDB)
  2. 确保数据库实际上位于服务器App_Data文件夹中
  3. 已更改连接字符串以在 app_data
  4. 中查找数据库

任何建议都很好。

连接字符串:

<connectionStrings 
    <add name="IgorsSellersToolBoxDB" 
         connectionString="metadata=res://*/Models.ModelDB.csdl|res://*/Models.ModelDB.ssdl|res://*/Models.ModelDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source= LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\IgorsSellersToolBoxDB.mdf;integrated security=True;multipleactiveresultsets=True;connect timeout=30;application name=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

错误:

Server error in '/WebToolbox' Application.

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. The specified LocalDB instance does not exist. )

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: 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. The specified LocalDB instance does not exist.)

Source error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.`

正如史蒂夫在评论中提到的,我已经确认在生产中使用 localDB 是不受欢迎的。

解法: 在我的开发机器上使用一个 SQL 服务器,在我的生产服务器上使用一个单独的服务器。我所要做的就是在我的 Web.Config.

中有单独的连接字符串

在你的web.config中,你可以在解决方案资源管理器中展开文件,你会看到两个文件:

  • Web.Debug.Config
  • Web.Release.Config

这里有一个很好的答案,解释得很好 -->Using different Web.config in development and production environment