【问题标题】:DataProtection.EntityFramework raise an expcetion with PostgresqlData Protection.Entity Framework 引发 Postgresql 异常
【发布时间】:2022-10-25 23:09:37
【问题描述】:

我对 Postgresql 数据库也有同样的问题。为了管理迁移,我使用的是 EF。尝试向应用程序添加迁移时,我收到了这个奇怪的问题。在我开始使用 SQL Server 之前,我从来没有遇到过这个问题。

System.Security.Cryptography.CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.
     ---> Npgsql.PostgresException (0x80004005): 42P01: relation "DataProtectionKey" does not exist
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
       at Npgsql.NpgsqlDataReader.NextResult()
       at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
       at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
       at System.Data.Common.DbCommand.ExecuteReader()
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
       at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.EntityFrameworkCoreXmlRepository`1.GetAllElements()
       at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.GetAllKeys()
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now, IKey keyJustAdded)
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.ICacheableKeyRingProvider.GetCacheableKeyRing(DateTimeOffset now)
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow, Boolean forceRefresh)
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRing()
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext)
      Exception data:
        Severity: ERROR
        SqlState: 42P01
        MessageText: relation "DataProtectionKey" does not exist
        Position: 22
        File: parse_relation.c
        Line: 1381
        Routine: parserOpenTable
       --- End of inner exception stack trace ---
       at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext)
       at Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Protect(IDataProtector protector, String plaintext)
       at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass3_0`2.<SanitizeConverter>b__0(Object v)
       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetValue(ColumnModification columnModification)
       at System.Linq.Enumerable.WhereSelectListIterator`2.ToList()
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(DiffContext diffContext)+MoveNext()
       at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
       at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

我在用:

  1. .net 核心 3.1
  2. EF 3.1.13
  3. Microsoft.AspNetCore.DataProtection.EntityFramework 3.1.13
  4. Npgsql.EntityFrameworkCore.PostgreSQL 3.1.11

    我的应用程序数据库上下文有这个定义

    ....
     public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
     public DbSet<DataAudit> AuditTrail { get; set; }
     public DbSet<Settings> Settings { get; set; }
     public DbSet<ApiResourceEntity> ApiResources { get; set; }
    ....
    

    DbContext 定义

    serviceCollection.AddDataProtection()
                    .SetApplicationName("MyAPP")
                    .PersistKeysToDbContext<ApplicationDbContext>()
                    .ProtectKeysWithDpapiNG();
    

    编辑:我评论了这段代码并且迁移已经通过,所以问题是 OnModelCreating(ModelBuilder modelBuilder) 方法上的这段代码:

     foreach (var entityType in modelBuilder.Model.GetEntityTypes())
                {
                    foreach (var property in entityType.GetProperties())
                    {
                        var attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptedAttribute), false);
                        if (attributes.Any())
                        {
                            property.SetValueConverter(new EncryptedConverter(_dataProtectionProvider));
                        }
                    }
                }
    

    任何想法都会有所帮助。 问候

【问题讨论】:

    标签: c# postgresql entity-framework asp.net-core


    【解决方案1】:

    首先,您需要运行 Skoruba project.Identity Server 4.Admin。他将推出迁移。然后才启动 Skoruba.Identity Server 4.STS.Identity!你可能正在做相反的事情......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 2012-04-07
      • 2010-12-19
      • 1970-01-01
      • 2019-02-27
      • 2021-07-09
      • 1970-01-01
      • 2021-01-28
      相关资源
      最近更新 更多