IdentityServer4.Admin icon indicating copy to clipboard operation
IdentityServer4.Admin copied to clipboard

Database tables not created. Invalid object name 'DataProtectionKeys'.

Open ajaypunekar1 opened this issue 4 years ago • 7 comments

I am trying to run the project with SqlServer as store. When the application runs, it throws errors related to DataProtectionKeys tables not being present. I have deleted existing repository migrations & run script from build folder i.e. add-migrations.ps1

After migration complete I have run the project but getting below error

[ERR] An exception occurred while iterating over the results of a query for context type 'Project.IdentityServer4.Admin.EntityFramework.Shared.DbContexts.IdentityServerDataProtectionDbContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'DataProtectionKeys'.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   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.SqlDataReader.TryConsumeMetaData()
   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()

Trying to resolve since last two days. How can I resolved this?

ajaypunekar1 avatar Oct 12 '21 18:10 ajaypunekar1

I have the same issue with MySQL engine. Looks like not all of the migrations get applied. Only the following ones succeeded:

  • 20191120085611_DbInit
  • 20191120085729_DbInit
  • 20201108173143_UpdateIdentityServerToVersion4

andreityuhai avatar Oct 13 '21 13:10 andreityuhai

Hey, can you check, that in your database is this table? You should verify that db migrations for dataprotection have been applied.

Thanks

skoruba avatar Oct 17 '21 07:10 skoruba

@skoruba Thanks for reply. I have tried below steps.

1: Removed migration folder 2: Updated my Azure Db connection string in all three projects(Note: There is no any table in my database) 3: Run migration script "add-migrations" which generate new migration folder. DataProtection migration is also generated. 4: Run all three project after migration. After that it gives me this error.

ajaypunekar1 avatar Oct 18 '21 04:10 ajaypunekar1

Check that you have allowed these values in your appsettings.json:

"SeedConfiguration": {
        "ApplySeed": true
    },
    "DatabaseMigrationsConfiguration": {
        "ApplyDatabaseMigrations": true
    },

skoruba avatar Oct 18 '21 08:10 skoruba

Yes, both this values set to true. I think, migration for DataProtectionKeys is not working. It is not creating table in database. Here is my migration file 20211018083501_DbInit.cs when I run add-migrations.sh1

public partial class DbInit : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "dbo");

            migrationBuilder.CreateTable(
                name: "DataProtectionKeys",
                schema: "dbo",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FriendlyName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Xml = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DataProtectionKeys", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "DataProtectionKeys",
                schema: "dbo");
        }
    }

ajaypunekar1 avatar Oct 18 '21 08:10 ajaypunekar1

Just discovered that there were some migration issues with EFCore. Update the Microsoft.EntityFrameworkCore.SqlServer to 5.0.11 and this will resolve it.

Norrch2 avatar Nov 05 '21 21:11 Norrch2

Just discovered that there were some migration issues with EFCore. Update the Microsoft.EntityFrameworkCore.SqlServer to 5.0.11 and this will resolve it.

After lot of failure, I prefer self migration instead of EFCore migration. I have created this table in my DB using Sql query. And run the application.

ajaypunekar1 avatar Nov 08 '21 11:11 ajaypunekar1