ASP.NET 核心 - 应用程序在发布后未连接到数据库

ASP.NET Core - Application not connecting to database after publishing

我创建了一个带有用户身份验证的简单 ASP.Net 核心应用程序(因此所有 Entity Framework 都已预加载到 Web 应用程序模板中)。它使用 appsettings.json 文件中的 connectionString 连接到我的数据库,设置 "data source = {computerName}\{serverName}"

数据库实例和Visual Studio位于同一台机器上。这在开发和调试模式下将数据返回到网络 api 时效果很好。当我发布它并尝试从域名访问该站点时,它确实允许我查看不需要数据库连接的网页,但其他需要数据库中数据的网页 returns 页面如下:

Error.
An error occurred while processing your request. Development Mode Swapping to Development environment will display more detailed information about the error that occurred.

Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application.

我尝试将 ASPNETCORE_ENVIRONMENT 的环境变量从开发更改为生产,但没有成功。

我已经尝试在我的 project.json 文件中将 appsettings.Production.json 添加到 publishOptions,即使没有文件 appsettings.Production.json,但这并没有帮助。

web.config 文件中添加环境变量无效 Deployment

我需要帮助让已发布的网站 api 从开发 Visual Studio 设置之外连接到我的 SQL 服务器数据库。

我最后想到的可能是我对连接字符串的理解不正确。如果网络 api 使用连接字符串从服务器端连接到数据库,那么它应该像在开发中调用 http://localhost:port# 时一样正常工作,因为它们都在同一台机器上。但是,如果数据库字符串需要基于客户端调用,那么它就必须包含域名和 IP 地址。 谁能告诉我是哪一个?

唯一想到的是在 IIS 管理器中有一些我没有做但需要做的事情。我在那里也看到了连接字符串,但不确定这是为了我们做什么,因为连接字符串在应用程序内部。另外也许我应该给应用程序某种授权来与数据库服务器通信,即使它们在同一台机器上也是如此???

经过大量研究,最终谷歌搜索 "how to deploy a web api in iis" 我能够从 Host ASP.NET Web API in IIS using Visual Studio Publish 了解到我需要添加安全实体 BUILTIN\IIS_IUSRS。然后放置到我的数据库表的映射,并将数据库的 db_datareader(可能 db_datawriter)提供给 IUSER,以允许从我的自托管 IIS Web api 进行访问。这来自上面提到的 link 部分说明

Accessing Database under IIS APPPOOL\ASP.NET v4.0 As we are using ASP.NET v4.0 App Pool make sure IIS APPPOOL\ASP.NET v4.0 is added to your Database Server -> Security -> Logins.