如何对 Postgres 9.0 数据库进行逆向工程

How to reverse engineer Postgres 9.0 database

ASP.NET Core 6 MVC 应用程序使用 EF Core 和 Npgsql。

逆向工程 Postgres 9.0 数据库使用

scaffolder.ScaffoldModel(connectionString.ToString(), dbOpts, modelOpts, codeGenOpts);

抛出异常

Npgsql.PostgresException (0x80004005): 42703: column "enumsortorder" does not exist

在 sql

SELECT
  nspname,
  typname,
  array_agg(enumlabel ORDER BY enumsortorder) AS labels
FROM pg_enum
JOIN pg_type ON pg_type.oid = enumtypid
JOIN pg_namespace ON pg_namespace.oid = pg_type.typnamespace
GROUP BY nspname, typname

有堆栈跟踪

at Npgsql.Internal.NpgsqlConnector.g__ReadMessageLong|213_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage) at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken) at Npgsql.NpgsqlDataReader.NextResult() at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal.NpgsqlDatabaseModelFactory.GetEnums(NpgsqlConnection connection, DatabaseModel databaseModel) at Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal.NpgsqlDatabaseModelFactory.Create(DbConnection dbConnection, DatabaseModelFactoryOptions options) at Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal.NpgsqlDatabaseModelFactory.Create(String connectionString, DatabaseModelFactoryOptions options) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)

如何在 .NET6 中对 Postgres 9.0 数据库进行逆向工程?枚举数据类型几乎不用。

我已经为 6.0.2 修复了这个问题,请参阅 https://github.com/npgsql/efcore.pg/issues/2160

最好不要在 github 和 Whosebug 上交叉 post 相同的问题 - 它主要会在两个地方创建更多的工作回答。如果某些东西看起来像 Npgsql 错误,那么 issue 就足够了,否则 Whosebug 对使用问题等很有用。