Identity Server 4 日志(无效的对象名称 'DeviceCodes')异常
Identity Server 4 loggs (Invalid object name 'DeviceCodes') exception
我们正在使用 Identity Server 4 进行身份验证。将我们的解决方案从 .Net Core 2.0 升级到 .Net Core 3.1 后,我们开始注意到当客户端重定向到我们的身份验证 URL 时记录了一个新的异常,这并没有影响这个过程,最后一切似乎都正常用户。例外情况是:
An exception occurred while iterating over the results of a query for context type 'IdentityServer4.EntityFramework.DbContexts.PersistedGrantDbContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'DeviceCodes'.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
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.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
ClientConnectionId:2b07bb46-18fe-4553-a79f-89b0186556d4
Error Number:208,State:1,Class:16
导致此异常的查询:
Failed executing DbCommand (2ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT [d].[UserCode], [d].[ClientId], [d].[CreationTime], [d].[Data], [d].[DeviceCode], [d].[Expiration], [d].[SubjectId]
FROM [DeviceCodes] AS [d]
WHERE [d].[Expiration] < GETUTCDATE()
ORDER BY [d].[DeviceCode]
我们的上下文中没有此 table 'DeviceCodes',请提供任何建议!
您缺少 DeviceCodes table,您将通过从程序包管理器控制台 window 添加迁移来获得(现在您已经升级),如下所示:
add-migration Core3Upgrade -Context PersistedGrantDbContext
(请记住根据您的设置在 VS 中设置正确的启动项目并在包管理器控制台中设置适当的默认项目,否则将失败。)
然后...
update-database -Context PersistedGrantDbContext
(或 运行 您可能用于 seed/apply 身份验证数据库迁移的任何初始化代码)
如果成功,应执行以下 SQL:
Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [DeviceCodes] (
[UserCode] nvarchar(200) NOT NULL,
[DeviceCode] nvarchar(200) NOT NULL,
[SubjectId] nvarchar(200) NULL,
[ClientId] nvarchar(200) NOT NULL,
[CreationTime] datetime2 NOT NULL,
[Expiration] datetime2 NOT NULL,
[Data] nvarchar(max) NOT NULL,
CONSTRAINT [PK_DeviceCodes] PRIMARY KEY ([UserCode])
);
我们正在使用 Identity Server 4 进行身份验证。将我们的解决方案从 .Net Core 2.0 升级到 .Net Core 3.1 后,我们开始注意到当客户端重定向到我们的身份验证 URL 时记录了一个新的异常,这并没有影响这个过程,最后一切似乎都正常用户。例外情况是:
An exception occurred while iterating over the results of a query for context type 'IdentityServer4.EntityFramework.DbContexts.PersistedGrantDbContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'DeviceCodes'.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
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.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
ClientConnectionId:2b07bb46-18fe-4553-a79f-89b0186556d4
Error Number:208,State:1,Class:16
导致此异常的查询:
Failed executing DbCommand (2ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT [d].[UserCode], [d].[ClientId], [d].[CreationTime], [d].[Data], [d].[DeviceCode], [d].[Expiration], [d].[SubjectId]
FROM [DeviceCodes] AS [d]
WHERE [d].[Expiration] < GETUTCDATE()
ORDER BY [d].[DeviceCode]
我们的上下文中没有此 table 'DeviceCodes',请提供任何建议!
您缺少 DeviceCodes table,您将通过从程序包管理器控制台 window 添加迁移来获得(现在您已经升级),如下所示:
add-migration Core3Upgrade -Context PersistedGrantDbContext
(请记住根据您的设置在 VS 中设置正确的启动项目并在包管理器控制台中设置适当的默认项目,否则将失败。)
然后...
update-database -Context PersistedGrantDbContext
(或 运行 您可能用于 seed/apply 身份验证数据库迁移的任何初始化代码)
如果成功,应执行以下 SQL:
Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [DeviceCodes] (
[UserCode] nvarchar(200) NOT NULL,
[DeviceCode] nvarchar(200) NOT NULL,
[SubjectId] nvarchar(200) NULL,
[ClientId] nvarchar(200) NOT NULL,
[CreationTime] datetime2 NOT NULL,
[Expiration] datetime2 NOT NULL,
[Data] nvarchar(max) NOT NULL,
CONSTRAINT [PK_DeviceCodes] PRIMARY KEY ([UserCode])
);