Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add parameter for postgres enum types in C#

Tags:

c#

postgresql

Hi currently have the problem, that I want to insert values in a postgres database where the table contains self defined types like.

CREATE TYPE TestEnum AS ENUM ('Value1','Value2');

When I try to add a parameter in C# I always get an error because of the wrong NpgsqlDbType. So my question is what NpgsqlDbType to use for such self defined Types.

var parameter = new NpgsqlParameter(":p1", NpgsqlDbType.????)
{
    Value = "Value1",
    Direction = ParameterDirection.Input
}

Thanks for your help. I'm really going mad because of this problem.

like image 334
BitKFu Avatar asked Sep 07 '25 19:09

BitKFu


1 Answers

After all, I found a solution that solves the problem, although it's not the real solution. I now add a NpgsqlDbType.Varchar parameter and add a CAST(:p1 as "TestEnum") to the SQL

e.g.

INSERT INTO tableName (Col) VALUES ( CAST(:p1 as "TestEnum") )

It works for me, although I do not think that this is a very nice solution due to the cast. If someone will find a better solution in future, please drop me a line. ;)

like image 151
BitKFu Avatar answered Sep 09 '25 11:09

BitKFu