我在尝试访问实体框架的本地 SQL 数据库时收到此错误。 如果有任何有帮助的提示,我将不胜感激。
生产服务器:IIS 10、Windows Server 2016 上的 ASPNET Core 7 Web 应用程序。 实体机。
在 VS 2022 上开发。 还在同一服务器上运行 SQL Express,从同一应用程序进行访问。
Windows AppPool 用户可以访问 SQL Express,但无法访问 (localdb)\mssqllocaldb 中的数据库(错误消息显示“无法打开数据库”,即登录有效)。
我已在我的开发计算机上运行此程序,创建了 EF 数据库,一切正常。 部署到服务器上,IIS Worker似乎无法访问EF数据库。
我可以使用 SQL Management Studio 18 登录生产服务器和开发计算机。登录名/用户和权限似乎合适。
连接字符串:
Server=(localdb)\\mssqllocaldb;Database=aspnet;Trusted_Connection=True;MultipleActiveResultSets=true;
在program.cs中我有这个代码行,在app.run()之前的几行:
if (await userManager.FindByEmailAsync(sUserEmail) == null)
这会触发 EF 数据库查询。 这一行是抛出主题中的异常的地方(下面是完整的异常文本)。
到目前为止我已采取的步骤(没有什么区别)
我一直在阅读有关身份的自定义存储的内容。 基本上我的下一步是尝试在 SQL Express 中重新创建数据库,并修改连接字符串以指向那里并尝试。实际上,我更喜欢只用一个 SQL 实例来处理所有事情,所以如果这对我来说可行的话可能会更好。
但我很想知道并消除阻止数据库连接的障碍。 为了澄清起见,我没有在这里写下数据库和应用程序池用户的全名。
如果我可以提供任何其他信息可以帮助解决此问题,请告诉我。
例外:
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "aspnet" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\'.
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
ClientConnectionId:1a0e38a7-1bee-4d50-b1f4-2a10fdd3e7aa
Error Number:4060,State:1,Class:11
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
有几点需要注意:
在本地计算机上复制此设置。设置 IIS 来镜像您的生产服务器并以这种方式访问您的本地主机应用程序...这将使故障排除更加容易。
删除不受信任的连接字符串并使用完全定义的用户/密码:
<add name={name} connectionString="Data Source={server_name};Initial Catalog={database_name};MultipleActiveResultSets=True;User Id={user};Password={password}" providerName="System.Data.SqlClient" />
。如果您决定这样做,请确保您有一个登录映射到 SQL Server 中的用户:{server_name}/Security/Logins
检查 SQL Server 中的权限。通过 IIS 应用程序池连接的用户的权限应如下所示:
如果上述任何内容有帮助,请告诉我,然后我们就可以从那里开始。