NPGSQL枚举映射

发布于 2025-02-07 05:45:25 字数 1721 浏览 1 评论 0原文

我正在使用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 技术交流群。

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

发布评论

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

评论(1

后来的我们 2025-02-14 05:45:25

我认为这是不值得的。 如果没有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

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