Repodb: Bug: The ENUM type of Postgre (DB) is not being handled properly in RepoDb.PostgreSql.

Created on 1 Mar 2020  路  4Comments  路  Source: mikependon/RepoDB

The codes below must be working, but it is now.

using Npgsql;
using PostgreSqlEnumTests.Enumerations;
using RepoDb;
using RepoDb.Attributes;
using System;

namespace PostgreSqlEnumTests
{
    class Program
    {
        [Map("person")]
        private class Person
        {
            public long id { get; set; }
            public string name { get; set; }
            public Hands usedhands { get; set; }
        }

        private const string m_connectionString = "Server=127.0.0.1;Port=5432;Database=RepoDb;User Id=postgres;Password=Password123;";

        static void Main(string[] args)
        {
            PostgreSqlBootstrap.Initialize();
            CreateEnumType();
            DropTable();
            CreateTable();
            InsertPerson();
            QueryPeople();
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }

        static void CreateEnumType()
        {
            return; // Already created
            using (var connection = new NpgsqlConnection(m_connectionString))
            {
                connection.ExecuteNonQuery("CREATE TYPE Hands AS ENUM ('Left', 'Right');");
            }
        }

        static void DropTable()
        {
            return; // Already dropped
            using (var connection = new NpgsqlConnection(m_connectionString))
            {
                connection.ExecuteNonQuery(@"DROP TABLE public.Person;");
            }
        }

        static void CreateTable()
        {
            using (var connection = new NpgsqlConnection(m_connectionString))
            {
                connection.ExecuteNonQuery(@"CREATE TABLE IF NOT EXISTS public.Person
                    (
                        Id bigint GENERATED ALWAYS AS IDENTITY,
                        Name Text,
                        UsedHands Hands
                    )

                    TABLESPACE pg_default;

                    ALTER TABLE public.Person
                        OWNER to postgres;");
            }
        }

        static void InsertPerson()
        {
            using (var connection = new NpgsqlConnection(m_connectionString))
            {
                var id = connection.Insert(new Person
                {
                    name = Guid.NewGuid().ToString(),
                    usedhands = Hands.Left
                });
            }
        }

        static void QueryPeople()
        {
            using (var connection = new NpgsqlConnection(m_connectionString))
            {
                var people = connection.QueryAll<Person>();
                foreach (var person in people)
                {
                    Console.WriteLine($"{person.id}. {person.name} ({person.usedhands})");
                }
            }
        }
    }
}
bug todo

All 4 comments

This is related to what you reported in #390. I myself encountered this and trying to understand more why it is passing on my Integration Tests and failing on this small project. @ffandreassoroko

@ffandreassoroko - I now have fixed this on my DEV machine by setting the NpgsqlDbType property to Unknown.

[Map("person")]
private class Person
{
    public long id { get; set; }
    public string name { get; set; }
    [NpgsqlTypeMapAttribute(NpgsqlDbType.Unknown)]
    public Hands usedhands { get; set; }
}

That should also fix yours.

yes, that works!

Great. I will close this one now. Thanks!

Was this page helpful?
0 / 5 - 0 ratings