简体   繁体   中英

NpgsqlException : “Unknown message code: 74” on dotnet ef database update

When I tried to update(build tables from Models) the PostgreSQL database for the first time I caught this exception:

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.Op erationBase.Execute(Action action) Unknown message code: 74

unfortunately, I couldn't find any clue about this exception, and as you see the exception is totally unclear.

After hours of investigation and reading similar errors and study the source code of the PGUtil.cs , I suspected to the communication problem.

And Bingo. the root of the problem was the wrong port number committed by my colleague.

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);
        }
    }

When I changed the port number to the correct one everything goes well:

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

I know it might be totally unrelated to the Exception message and because of that I share it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM