Npgsql.PostgresException (0x80004005): XX000: 架构删除时类型 207852 的缓存查找失败

Npgsql.PostgresException (0x80004005): XX000: cache lookup failed for type 207852 when schema dropped

我',正在运行我的测试套件并尝试使用枚举:

CREATE TYPE dockind AS ENUM ('I', 'O', 'Q');
CREATE TABLE document (
    doc_id       SERIAL,
    doc_kind     dockind NOT NULL DEFAULT 'O',
)

let openConn() =
    let conStr = localConnectionString()
    let conn = new NpgsqlConnection(conStr)
    conn.Open()


    conn :> IDbConnection
//Called after the schema is created
let initDb (conn:IDbConnection) =
    let db = conn :?> NpgsqlConnection
    db.TypeMapper.MapEnum<DocKind>("dockind") |> ignore

但是,我遇到了一个奇怪的错误:

Npgsql.PostgresException (0x80004005): XX000: cache lookup failed for type 207852
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 444
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1221
   at Npgsql.NpgsqlCommand.ExecuteScalar(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1086
   at Npgsql.NpgsqlCommand.ExecuteScalar() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1063
   at CoreLib.Db.queryScalar[a,b](a con, String sql, IEnumerable`1 parameters) in /Users/mamcx/Proyectos/BestSellerPOS/CoreLib/DbStore.fs:line 184

Npgsql 版本 4.0.0


这发生在我删除架构并重建它时。如果我不重建它,它就可以工作。那么如何解决这个问题?

let createDb() =
    use con = openConn()
    let sql = openDbFile()
    exeSql con sql |> ignore

let testDb() =
    DbServer.createDb()
    let db = DbServer.openConn()
    DbServer.initDb(db)

我没有使用重用连接来构建数据库和执行测试。

Npgsql 在第一次连接给定数据库时缓存所有 PostgreSQL 类型(类型缓存在连接字符串上)。如果您对类型进行任何更改,例如定义一个新类型或删除一个类型,您必须通过调用 NpgsqlConnection.ReloadTypes().

告诉 Npgsql 刷新类型缓存