Identityserver 4 Combined_AspNetIdentity_and_EntityFrameworkStorage

0

i try the example of identityserver 4 and Asp.net Identity and Entity framework Storage.(see here: https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/Combined_AspNetIdentity_and_EntityFrameworkStorage/src/IdentityServerWithAspIdAndEF ) I changed the SQL Connection to MySql and run the example. the example creates the tables but, if he wants to fill the table with data i get errors:

    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

it´s seems that something goes wrong with the id. what can i do to solve this problem?

these lines i have changed in the 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
                });
entity-framework
asp.net-identity
identityserver4
asked on Stack Overflow Jul 14, 2018 by eldios1981 • edited Jul 14, 2018 by eldios1981

1 Answer

1

The problem is that Id has no value. You do not insert a value for Id (which is good), but since it is a required field and no default value was defined you'll receive this message.

The reason is that the Id column isn't set as AUTO_INCREMENT. You can solve this issue by adjusting the migration model, currently in 20180109200721_InitialIdentityServerConfigurationDbMigration.cs:

migrationBuilder.CreateTable(
    name: "Clients",
        columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                          .Annotation("Sqlite:Autoincrement", true),

Change the annotation line to:

.Annotation("MySql:ValueGenerationStrategy", 
            MySqlValueGenerationStrategy.IdentityColumn)

You will have to adjust this for all tables (with autoincrement) in all migration scripts, please note the scripts in the sub directories. A global replace may help you here.

You may want to start with a new database to generate the model. Please let me know if this solves the issue.

--- update links ---

migration model (20181230131608_InitialIdentityServerConfigurationDbMigration)

migration scripts

answered on Stack Overflow Jul 14, 2018 by Ruard van Elburg • edited Jan 23, 2019 by Ruard van Elburg

User contributions licensed under CC BY-SA 3.0