Entity framework Identityserver 4作为网络身份和实体框架存储的组合

Entity framework Identityserver 4作为网络身份和实体框架存储的组合,entity-framework,asp.net-identity,identityserver4,Entity Framework,Asp.net Identity,Identityserver4,我尝试了identityserver 4和Asp.net Identity and Entity framework存储的示例。请参见此处: 我将SQL连接更改为MySql并运行该示例。该示例创建了表,但如果他想用数据填充表,则会出现以下错误: Identity | [12:41:08 Error] Microsoft.EntityFrameworkCore.Database.Command Identity | Failed executing Db

我尝试了identityserver 4和Asp.net Identity and Entity framework存储的示例。请参见此处: 我将SQL连接更改为MySql并运行该示例。该示例创建了表,但如果他想用数据填充表,则会出现以下错误:

    Identity          | [12:41:08 Error] Microsoft.EntityFrameworkCore.Database.Command
Identity          | Failed executing DbCommand (25ms) [Parameters=[@p0='?' (DbType = Int32), @p1='?' (DbType = Int32), @p2='?' (DbType = Int32), @p3='?' (DbType = Boolean), @p4='?' (DbType = Boolean), @p5='?' (DbType = Boolean), @p6='?' (DbType = Boolean), @p7='?' (DbType = Boolean), @p8='?' (DbType = Boolean), @p9='?' (DbType = Int32), @p10='?' (DbType = Boolean), @p11='?' (Size = 2000), @p12='?' (Size = 200), @p13='?' (Size = 200), @p14='?' (Size = 200), @p15='?' (Size = 2000), @p16='?' (DbType = Int32), @p17='?' (Size = 1000), @p18='?' (DbType = Boolean), @p19='?' (DbType = Boolean), @p20='?' (DbType = Boolean), @p21='?' (Size = 2000), @p22='?' (DbType = Int32), @p23='?' (DbType = Boolean), @p24='?' (Size = 2000), @p25='?' (Size = 200), @p26='?' (Size = 200), @p27='?' (DbType = Int32), @p28='?' (DbType = Int32), @p29='?' (DbType = Boolean), @p30='?' (DbType = Boolean), @p31='?' (DbType = Boolean), @p32='?' (DbType = Int32), @p33='?' (DbType = Boolean)], CommandType='Text', CommandTimeout='30']
Identity          | INSERT INTO `Clients` (`AbsoluteRefreshTokenLifetime`, `AccessTokenLifetime`, `AccessTokenType`, `AllowAccessTokensViaBrowser`, `AllowOfflineAccess`, `AllowPlainTextPkce`, `AllowRememberConsent`, `AlwaysIncludeUserClaimsInIdToken`, `AlwaysSendClientClaims`, `AuthorizationCodeLifetime`, `BackChannelLogoutSessionRequired`, `BackChannelLogoutUri`, `ClientClaimsPrefix`, `ClientId`, `ClientName`, `ClientUri`, `ConsentLifetime`, `Description`, `EnableLocalLogin`, `Enabled`, `FrontChannelLogoutSessionRequired`, `FrontChannelLogoutUri`, `IdentityTokenLifetime`, `IncludeJwtId`, `LogoUri`, `PairWiseSubjectSalt`, `ProtocolType`, `RefreshTokenExpiration`, `RefreshTokenUsage`, `RequireClientSecret`, `RequireConsent`, `RequirePkce`, `SlidingRefreshTokenLifetime`, `UpdateAccessTokenClaimsOnRefresh`)
Identity          | VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28, @p29, @p30, @p31, @p32, @p33);
Identity          | SELECT `Id`
Identity          | FROM `Clients`
Identity          | WHERE ROW_COUNT() = 1 AND `Id` = LAST_INSERT_ID();
Identity          | MySql.Data.MySqlClient.MySqlException (0x80004005): Field 'Id' doesn't have a default value ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Field 'Id' doesn't have a default value
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
Identity          |    at MySqlConnector.Core.ResultSet.<ReadResultSetHeaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 44
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 93
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 328
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<CreateAsync>d__89.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 318
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 73
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 168
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
Identity          | 
Identity          | [12:41:08 Error] Microsoft.EntityFrameworkCore.Update
Identity          | An exception occurred in the database while saving changes for context type 'IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext'.
Identity          | Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
Identity          |    at MySqlConnector.Core.ResultSet.<ReadResultSetHeaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 44
Identity          |    --- End of inner exception stack trace ---
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 93
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 328
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<CreateAsync>d__89.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 318
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 73
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 168
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    --- End of inner exception stack trace ---
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
Identity          |    at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          |    at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          | Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
Identity          |    at MySqlConnector.Core.ResultSet.<ReadResultSetHeaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 44
Identity          |    --- End of inner exception stack trace ---
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 93
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 328
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<CreateAsync>d__89.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 318
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 73
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 168
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    --- End of inner exception stack trace ---
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
Identity          |    at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          |    at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          | 
Identity          | 
Identity          | Unhandled Exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value ---> MySql.Data.MySqlClient.MySqlException: Field 'Id' doesn't have a default value
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
Identity          |    at MySqlConnector.Core.ResultSet.<ReadResultSetHeaderAsync>d__1.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\ResultSet.cs:line 44
Identity          |    --- End of inner exception stack trace ---
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 93
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 328
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlDataReader.<CreateAsync>d__89.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 318
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 73
Identity          | --- End of stack trace from previous location where exception was thrown ---
Identity          |    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Identity          |    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Identity          |    at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 168
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    --- End of inner exception stack trace ---
Identity          |    at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
Identity          |    at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
Identity          |    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
Identity          |    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          |    at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Identity          |    at IdentityServerWithAspIdAndEF.SeedData.EnsureSeedData(ConfigurationDbContext context) in /app/SeedData.cs:line 127
Identity          |    at IdentityServerWithAspIdAndEF.SeedData.EnsureSeedData(IServiceProvider serviceProvider) in /app/SeedData.cs:line 33
Identity          |    at IdentityServerWithAspIdAndEF.Program.Main(String[] args) in /app/Program.cs:line 42
Identity exited with code 139
似乎id出现了问题。我能做些什么来解决这个问题

我在startup.cd中更改了以下行:

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(connectionString));

.AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = b =>
                        b.UseMySql(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                // this adds the operational data from DB (codes, tokens, consents)
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = b =>
                        b.UseMySql(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));

                    // this enables automatic token cleanup. this is optional.
                    options.EnableTokenCleanup = true;
                    // options.TokenCleanupInterval = 15; // frequency in seconds to cleanup stale grants. 15 is useful during debugging
                });
问题是Id没有值。您没有为Id插入一个好的值,但由于它是必填字段,并且没有定义默认值,您将收到此消息

原因是Id列未设置为自动增量。您可以通过调整当前位于20180109200721_InitialIdentityServerConfigurationDbMigration.cs中的:

migrationBuilder.CreateTable(
    name: "Clients",
        columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                          .Annotation("Sqlite:Autoincrement", true),
您将不得不对中包含autoincrement的所有表进行调整,请注意子目录中的脚本。在这里,全球替换可能会对您有所帮助

您可能希望从一个新的数据库开始生成模型。请让我知道这是否解决了问题

--更新链接--

20181230131608_初始标识服务器配置迁移


非常感谢你。这就是解决办法。它现在工作得很好。谢谢
.Annotation("MySql:ValueGenerationStrategy", 
            MySqlValueGenerationStrategy.IdentityColumn)