You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convert unmapped enums into the database type as their underlying integer type #5016
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:
createtableperson
(
name textnot null,
category integernot null
);
Suppose, we also have a C# enum which logically (but not physically) maps onto the person.category:
publicenumPersonCategory{Friends=1,Relatives=2}
Consider the following test:
[Test]publicasyncTaskFilterWithEnumAsObjectTest_NpgSql(){varconnectionString="...";awaitusingvarconn=newNpgsqlConnection(connectionString);awaitconn.OpenAsync();awaitusingvarcommand=newNpgsqlCommand("SELECT * FROM person where category = $1",conn);varparameter=newNpgsqlParameter("$1",PersonCategory.Friends);command.Parameters.Add(parameter);awaitusingvarreader=awaitcommand.ExecuteReaderAsync();awaitreader.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.
@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?
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.
Activity
vonzshik commentedon Mar 27, 2023
@roji duplicate of #3303?
ademchenko commentedon May 4, 2023
@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?
roji commentedon May 5, 2023
Duplicate of #3303
roji commentedon May 5, 2023
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.