使用 group by 时 npgsql 数据类型未知

npgsql data type unknown when using group by

我有 2 个表:

CREATE TABLE "book" 
(
  "id" serial PRIMARY KEY, 
  "ean_number" TEXT NULL, 
  "title" TEXT NULL 
); 

CREATE TABLE "e_book" 
(
  "id" serial PRIMARY KEY, 
  "ean" TEXT NULL, 
  "title" TEXT NULL, 
  "format" VARCHAR(255) NOT NULL, 
  "physical_book_ean" TEXT NULL 
); 

书籍与 e_book 之间存在一对多或 none 关系。

我想在我的代码中运行这样的查询:

var q = "select b.*, array_agg(e) ebooks from book b " +
         "left join e_book e on e.physical_book_ean = b.ean_number " +
         "group by b.id";

using (var cmd = new NpgsqlCommand(q, conn))
using (var reader = cmd.ExecuteReader())
    while (reader.Read())
    {
        //read data
    }

array_agg 栏电子书作为内容类型出现 <unknown>

如何定义内容类型以便阅读?

这是作为 github 问题打开的:https://github.com/npgsql/npgsql/issues/2510

那里给出的答案:

First, if you create the e_book table and query it in the same process, you need to tell Npgsql to reload database type definitions. This is because when Npgsql first connects to a database, it loads the list of types and caches it - but at that point the e_book type doesn't exist yet. If you run your application again with the tables already there you should no longer have this issue, or you can call Npgsql.ReloadTypes().

Second, you will need to pass the LoadTableComposites=true flag on the connection string, telling Npgsql to load all composite types - including those which correspond to tables. Npgsql doesn't do that by default since the number of tables can be massive and affect startup performance in some cases.