NPGSQL枚举映射
我正在使用Dapper 2.0.123,Dapper.contrib 2.0.78和NPGSQL 6.0.4在ASP.NET API(.NET 6)项目中。 Postgres数据库中的表具有列状态
定义为campaigns_status_enum
。班级及其枚举看起来像这样:
[Table("campaigns")]
public class Campaign
{
[Key]
public int id { get; set; }
// ommited, irrelevant to this question
//[NpgsqlTypes.PgName("campaigns_status_enum")]
public ECampaignStatus status { get; set; }
public enum ECampaignStatus
{
[NpgsqlTypes.PgName("active")]
active,
[NpgsqlTypes.PgName("inactive")]
inactive,
[NpgsqlTypes.PgName("draft")]
draft
}
}
数据库中的枚举是这样的:
CREATE TYPE public.campaigns_status_enum AS ENUM
('active', 'inactive', 'draft');
在其他问题中,在此处以及NPGSQL 文档我已经读到,我需要使用npgsqlconnection.globaltypemapper
,并且在此中,在我的startup.cs
中,我设置了映射像这样:
NpgsqlConnection.GlobalTypeMapper.MapEnum<Common.Campaigns.Campaign.ECampaignStatus>("campaigns_status_enum");
内部配置
,在注入databaseservice
处理连接等之前
IDbConnection db = new NpgsqlConnection(ConnectionString);
if (db.State != ConnectionState.Open)
{
db.Open();
((NpgsqlConnection)db).ReloadTypes();
}
。当我尝试插入
或update
使用dapper.contrib functions updateAsync,insertasync
{"42804: column \"status\" is of type campaigns_status_enum but expression is of type integer\r\n\r\nPOSITION: 172"}
,为什么这个.net枚举未映射到Postgres Enum?
I'm using Dapper 2.0.123, Dapper.Contrib 2.0.78 and Npgsql 6.0.4 in ASP.Net API (.Net 6) project. Table in Postgres database has column status
defined as campaigns_status_enum
. Class and its enum looks like this:
[Table("campaigns")]
public class Campaign
{
[Key]
public int id { get; set; }
// ommited, irrelevant to this question
//[NpgsqlTypes.PgName("campaigns_status_enum")]
public ECampaignStatus status { get; set; }
public enum ECampaignStatus
{
[NpgsqlTypes.PgName("active")]
active,
[NpgsqlTypes.PgName("inactive")]
inactive,
[NpgsqlTypes.PgName("draft")]
draft
}
}
Enum in database is defined like this:
CREATE TYPE public.campaigns_status_enum AS ENUM
('active', 'inactive', 'draft');
In other questions here and in Npgsql documentation I've read that I need to use NpgsqlConnection.GlobalTypeMapper
and according to that, in my Startup.cs
I've set the mapping like this:
NpgsqlConnection.GlobalTypeMapper.MapEnum<Common.Campaigns.Campaign.ECampaignStatus>("campaigns_status_enum");
inside ConfigureServices
, before injecting DatabaseService
which handles connection etc. I've also set
IDbConnection db = new NpgsqlConnection(ConnectionString);
if (db.State != ConnectionState.Open)
{
db.Open();
((NpgsqlConnection)db).ReloadTypes();
}
inside this DatabaseService
but apparently it has no effect. When I try to INSERT
or UPDATE
that table using Dapper.Contrib functions UpdateAsync, InsertAsync
, I get following exception:
{"42804: column \"status\" is of type campaigns_status_enum but expression is of type integer\r\n\r\nPOSITION: 172"}
What I'm doing wrong, why this .Net enum isn't mapped to Postgres enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不值得的。 如果没有Postgres枚举,我会保持简单,
任何其他映射C#枚举都将映射到整数。您的实体具有枚举属性,并将其映射到数据库中的简单数字
I don't think it's worth it. i would keep it simple
without postgres enums any additional mappings c# enum would be mapped to integer. your entity has enum property and it maps to simple number in database