我尝试了identityserver 4和Asp.net Identity and Entity framework Storage的例子。(请参见此处:https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/Combined_AspNetIdentity_and_EntityFrameworkStorage/src/IdentityServerWithAspIdAndEF(我将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:projectsmysqlconnectorsrcMySqlConnectorCoreResultSet.cs:line 44
Identity | at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.cs:line 93
Identity | at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorCoreTextCommandExecutor.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlCommand.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:projectsmysqlconnectorsrcMySqlConnectorCoreResultSet.cs:line 44
Identity | --- End of inner exception stack trace ---
Identity | at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.cs:line 93
Identity | at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorCoreTextCommandExecutor.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlCommand.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:projectsmysqlconnectorsrcMySqlConnectorCoreResultSet.cs:line 44
Identity | --- End of inner exception stack trace ---
Identity | at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.cs:line 93
Identity | at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorCoreTextCommandExecutor.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlCommand.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:projectsmysqlconnectorsrcMySqlConnectorCoreResultSet.cs:line 44
Identity | --- End of inner exception stack trace ---
Identity | at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.cs:line 93
Identity | at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__90.MoveNext() in C:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlDataReader.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:projectsmysqlconnectorsrcMySqlConnectorCoreTextCommandExecutor.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:projectsmysqlconnectorsrcMySqlConnectorMySql.Data.MySqlClientMySqlCommand.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好像出了问题。我该怎么办才能解决这个问题?
我在启动时更改了这些行。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列没有设置为AUTO_INCREMENT。您可以通过调整迁移模型来解决这个问题,目前在20180109200721_InitialIdentityServerConfigurationDbMigration.cs:
migrationBuilder.CreateTable(
name: "Clients",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
将注释行更改为:
.Annotation("MySql:ValueGenerationStrategy",
MySqlValueGenerationStrategy.IdentityColumn)
您必须为所有迁移脚本中的所有表(使用自动增量(调整此值,请注意子目录中的脚本。全球替代可能会对您有所帮助。
您可能需要从一个新的数据库开始生成模型。如果这能解决问题,请告诉我。
---更新链接---
迁移模型(20181230131608_InitialIdentityServerConfigurationDbMigration(
迁移脚本