Convert unmapped enums into the database type as their underlying integer type · Issue #5016 · npgsql/npgsql · GitHub
Skip to content

Convert unmapped enums into the database type as their underlying integer type #5016

Closed as duplicate

Description

@ademchenko

Steps to reproduce

Currently, there is a possibility to work with unmapped enums on the direction database -> C# code. But the backward direction is not possible: sometimes it's really worthwhile to have the ability to send the C# enum which doesn't have the corresponding postgres enum with automatic conversion of that C# enum into the underlying integer. The practical example is described here (as the issue of the linq2db project). So, let me just define the test which shows the technical problem.

Suppose, we have a database with the table:

create table person
(
    name     text    not null,
    category integer not null
);

Suppose, we also have a C# enum which logically (but not physically) maps onto the person.category:

public enum PersonCategory
{
    Friends = 1,
    Relatives = 2
}

Consider the following test:

 [Test]
    public async Task FilterWithEnumAsObjectTest_NpgSql()
    {
        var connectionString = "...";

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

        await using var command = new NpgsqlCommand("SELECT * FROM person where category = $1", conn);

        var parameter = new NpgsqlParameter("$1", PersonCategory.Friends);

        command.Parameters.Add(parameter);

        await using var reader = await command.ExecuteReaderAsync();
        await reader.ReadAsync();
    }

That test fails with the exception:
System.ArgumentException : A PostgreSQL type with the name 'person_category' was not found in the database

But what would be really worthwhile is to automatically convert enums that don't have the corresponding postgres enums into their underlying integer types prior to sending them to the database as parameters. It's quite a common case because integers are heavily used in postgres databases instead of enums, but in C# code it's good to have a little bit more strongly-typed conventions with enums.
So that feature could help a lot.

Activity

  1. vonzshik commented on Mar 27, 2023

    @vonzshik
    Contributor

    @roji duplicate of #3303?

  2. ademchenko commented on May 4, 2023

    @ademchenko
    Author

    @vonzshik , @roji this is a really important issue for us. Is that possible to push it? At least, can you, team, decide if it's duplicate of #3303 or not?

  3. roji commented on May 5, 2023

    @roji
    Member

    Duplicate of #3303

  4. roji commented on May 5, 2023

    @roji
    Member

    Yeah, this indeed looks like a duplicate of of #3303 - PR #4838 is also out to provide this.

    To avoid any confusion, note that we're treating this as an opt-in thing only, i.e. in #4838 you must explicitly specify that you want to write the enum as an int (or string) by specifying NpgsqlDbType. That means that you can't just set an enum value on a parameter and have it sent as an integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions