NpgsqlException:"Unknown message code: 74" 更新 dotnet ef 数据库

NpgsqlException : "Unknown message code: 74" on dotnet ef database update

当我第一次尝试更新(从模型构建表)PostgreSQL 数据库时,我发现了这个异常:

dotnet ef database update

Npgsql.NpgsqlException (0x80004005): Unknown message code: 74 at Npgsql.Util.PGUtil.ValidateBackendMessageCode(BackendMessageCode code) in C:\projects\npgsql\src\Npgsql\Util\PGUtil.cs:line 63 at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 894 --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnector.Authenticate(String username, NpgsqlTimeout timeout, Boolean async) in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.Auth.cs:line 22 at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 393 at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<g__OpenLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:line 241 --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnection.Open() in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:line 119 at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists() at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists() at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Unknown message code: 74

不幸的是,我找不到关于此异常的任何线索,而且如您所见,该异常完全不清楚。

经过数小时的调查和阅读类似错误并研究 PGUtil.cs 的源代码,我怀疑是通信问题。

还有宾果游戏。问题的根源是我的同事错误的端口号

public class IdentityResourceContextFactory : IDesignTimeDbContextFactory<IdentityResourceContext>
    {
        public IdentityResourceContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<IdentityResourceContext>();
            optionsBuilder.UseNpgsql("Username=postgres;Password=p@$$word;Host=localhost;Port=3306;Database=Identity;");

            return new IdentityResourceContext(optionsBuilder.Options);
        }
    }

当我将端口号更改为正确的端口号时,一切顺利:

Username=postgres;Password=p@$$word;Host=localhost;Port=5432;Database=Identity;

我知道它可能与异常消息完全无关,因此我在这里分享它。