如何将来自Postgres的枚举数组读取到C#中的枚举列表?

发布于 2025-02-01 00:38:33 字数 1832 浏览 3 评论 0原文

NET CORE 6.0使用NPGSQL v6.0.4。

表:


CREATE TABLE kontrolle.negativtext (
    id serial NOT NULL,
    check_doc "_negative_doc_check" NULL DEFAULT 
    CONSTRAINT negativtext_pkey PRIMARY KEY (id)
);

check_doc是自定义枚举类型的数组_negation_doc_check

枚举:

select enum_range(null::negative_doc_check)

返回{content_html_dirty,content_html_clean,content_txt_dirty,content_txt_clean}

代码>代码:

using System;
using System.Collections.Generic;
using Npgsql;

var constring = "SERVER=192.168.2.121;DATABASE=myuser;USER ID=mydb;PASSWORD=mypasswd;";
NpgsqlConnection.GlobalTypeMapper.MapEnum<negative_doc_check>("negative_doc_check");

await using var conn = new NpgsqlConnection(constring);
await conn.OpenAsync();

await using (var cmd = new NpgsqlCommand("SELECT check_doc from kontrolle.negativtext where id = 643", conn))
await using (var reader = await cmd.ExecuteReaderAsync()) {
    while (await reader.ReadAsync()) {
        var enumValue = reader.GetFieldValue<List<negative_doc_check>>(0);
        Console.WriteLine(enumValue);
    }
}

public enum negative_doc_check {
    CONTENT_HTML_DIRTY = 0,
    CONTENT_HTML_CLEAN = 1,
    CONTENT_TXT_DIRTY = 2,
    CONTENT_TXT_CLEAN = 4
}

public class Negativtext {
    public int Id { get; set; }
    public List<negative_doc_check> CheckDoc { get; set; }

}

exception:

system.invalidcastException:“接收到enum value'content_txt_dirty'database_d eymum to en eNUM_DOBS the Enmum the Enmum the Enmum to代码>

在我的C#代码和Postgres类型定义中,枚举值都是相同的。我是否缺少某些东西,还是不可能阅读 的列表?

net core 6.0 application using Npgsql v6.0.4.

Table:


CREATE TABLE kontrolle.negativtext (
    id serial NOT NULL,
    check_doc "_negative_doc_check" NULL DEFAULT 
    CONSTRAINT negativtext_pkey PRIMARY KEY (id)
);

Column check_doc is an array of the custom enum type _negative_doc_check.

Enum:

select enum_range(null::negative_doc_check)

returns {CONTENT_HTML_DIRTY,CONTENT_HTML_CLEAN,CONTENT_TXT_DIRTY,CONTENT_TXT_CLEAN}

Code:

using System;
using System.Collections.Generic;
using Npgsql;

var constring = "SERVER=192.168.2.121;DATABASE=myuser;USER ID=mydb;PASSWORD=mypasswd;";
NpgsqlConnection.GlobalTypeMapper.MapEnum<negative_doc_check>("negative_doc_check");

await using var conn = new NpgsqlConnection(constring);
await conn.OpenAsync();

await using (var cmd = new NpgsqlCommand("SELECT check_doc from kontrolle.negativtext where id = 643", conn))
await using (var reader = await cmd.ExecuteReaderAsync()) {
    while (await reader.ReadAsync()) {
        var enumValue = reader.GetFieldValue<List<negative_doc_check>>(0);
        Console.WriteLine(enumValue);
    }
}

public enum negative_doc_check {
    CONTENT_HTML_DIRTY = 0,
    CONTENT_HTML_CLEAN = 1,
    CONTENT_TXT_DIRTY = 2,
    CONTENT_TXT_CLEAN = 4
}

public class Negativtext {
    public int Id { get; set; }
    public List<negative_doc_check> CheckDoc { get; set; }

}

Exception:

System.InvalidCastException: "Received enum value 'CONTENT_TXT_DIRTY' from database which wasn't found on enum negative_doc_check"

The enum values are identical in both my C# code and the Postgres type definition. Am I missing something or is it just not possible to read a list of enums?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-02-08 00:38:33

默认情况下,当映射.NET映射到PG枚举时,NPGSQL假设.NET枚举被命名为标准.NET类型(即Pascal Case,即NativedOccheck,而不是负面_DOC_CHECK),而您的PG枚举则假定为snake case(ie natuge_doc_doc_check)。建议更改您的.NET枚举以符合这一点:

public enum NegativeDocCheck {
    ContentHtmlDirty = 0,
    ContentHtmlClean = 1,
    ContentTxtDirty = 2,
    ContentTxtClean = 4
}

否则,要保持非标准命名,您可以告诉NPGSQL按照映射枚举时的映射:

NpgsqlConnection.GlobalTypeMapper.MapEnum<negative_doc_check>("negative_doc_check", new NpgsqlNullNameTranslator());

By default, when mapping .NET to PG enums, Npgsql assumes .NET enums are named like standard .NET types (Pascal Case, i.e. NativeDocCheck and not negative_doc_check), whereas your PG enums are assumed to be snake case (i.e. negative_doc_check). It's recommended to change your .NET enum to conform to that:

public enum NegativeDocCheck {
    ContentHtmlDirty = 0,
    ContentHtmlClean = 1,
    ContentTxtDirty = 2,
    ContentTxtClean = 4
}

Otherwise, to keep your non-standard naming, you can tell Npgsql to map the names as is when mapping the enum:

NpgsqlConnection.GlobalTypeMapper.MapEnum<negative_doc_check>("negative_doc_check", new NpgsqlNullNameTranslator());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文