-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Null value types using the positional parameter API - guidance #286
Comments
It seems you are conflating positional parameters (with no name) and the generic The problem of passing null when using the generic |
Which non-generic variant should be used? I've tried: new NpgsqlParameter { NpgsqlDbType = NpgsqlDbType.Double, NpgsqlValue = nullableDouble } ;
// System.InvalidCastException: Parameter must be set
new NpgsqlParameter { NpgsqlValue = nullableDouble } ;
// System.InvalidOperationException: Parameter '$5' must have either its NpgsqlDbType or its DataTypeName or its Value set
new NpgsqlParameter { Value = nullableDouble } ;
// System.InvalidOperationException: Parameter '$5' must have either its NpgsqlDbType or its DataTypeName or its Value set
new NpgsqlParameter( nullableDouble ) ;
// Cannot resolve constructor 'NpgsqlParameter(System.Nullable<double>)', candidates are...
/* The only one I've gotten to work */
nullableDouble is {} nonNullableDouble ? new NpgsqlParameter{ NpgsqlValue = nonNullableDouble, NpgsqlDbType = NpgsqlDbType.Double } : new NpgsqlParameter{ Value = DBNull.Value } ; |
@erichiller at the moment, the last version is the only one that works: DBNull.Value is the way to represent a database null value. This is an unfortunate situation which I hope to resolve, but it's important to think about all this in the context of the .NET data API (ADO.NET), and not just Npgsql. In the meantime, you can have an extension method which adds a non-generic parameter with DBNull.Value for a null value, and a generic one with the non-null value otherwise. |
Thanks for the information. It might be good to have this in the official docs. I would be happy to add if you'd like. |
I've been writing all my new work using the positional
$1
style parameters and I've not found a good way of handling nullable values, particularly, it seems for value types.Previously something like this would work:
However, with positional parameters there is no parameter name.
Some methods I've tried:
Throws
InvalidCastException
I haven't found any tests that cover use of nulls with positional parameters, just named ones.
Using the boxing API (
Value =
orNpgsqlValue =
) is of course possible, but I'd like to avoid boxing.Any guidance would be great, ideally it would be in the docs, and I'd be happy to submit a PR for the docs if helpful.
Exception message:
Stack trace:
Further technical details
Npgsql version: 7.0.4
PostgreSQL version: 15.2
Operating system: Linux
The text was updated successfully, but these errors were encountered: