如何将来自Postgres的枚举数组读取到C#中的枚举列表?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,当映射.NET映射到PG枚举时,NPGSQL假设.NET枚举被命名为标准.NET类型(即Pascal Case,即NativedOccheck,而不是负面_DOC_CHECK),而您的PG枚举则假定为snake case(ie natuge_doc_doc_check)。建议更改您的.NET枚举以符合这一点:
否则,要保持非标准命名,您可以告诉NPGSQL按照映射枚举时的映射:
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:
Otherwise, to keep your non-standard naming, you can tell Npgsql to map the names as is when mapping the enum: