Skip to content
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

Dapper template doesn't work with collection parameters #144

Open
skolima opened this issue Sep 12, 2024 · 0 comments
Open

Dapper template doesn't work with collection parameters #144

skolima opened this issue Sep 12, 2024 · 0 comments

Comments

@skolima
Copy link

skolima commented Sep 12, 2024

I might be hitting a bug in Dapper, as I haven't found a nice way to resolve this.

I've got an ID type:

[StronglyTypedId(Template.Guid, "guid-dapper")]
public readonly partial struct MyId { }

and register the generated TypeHandler:

SqlMapper.AddTypeHandler(new MyId.DapperTypeHandler());

but when running a Dapper SQL query that uses a collection type parameter, not a singular parameter - let's say with criteria

"WHERE entity_id = ANY(@entityIds)"

Dapper (with Npgsql data access) throws the following exception:

System.InvalidCastException

Writing values of '<>z__ReadOnlySingleElementList`1[[Domain.Types.MyId]]' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type..
   at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForWriting>g__ThrowWritingNotSupported|1_0(Type type, PgSerializerOptions options, Nullable`1 pgTypeId, Nullable`1 npgsqlDbType, Exception inner)
   at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForWriting(Type type, Nullable`1 pgTypeId, PgSerializerOptions options, Nullable`1 npgsqlDbType)
   at Npgsql.NpgsqlParameter.ResolveTypeInfo(PgSerializerOptions options)
   at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType)
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 434

(this particular parameter was [new MyId(value)]) but it fails same way for every collection type.

A workaround I'm using currently is instead passing the collection parameters in as

entityIds = ids.Select(i=>i.value).ToArray()

I was looking how the dapper template would need to be extended to cater for collection parameters but failed to get it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant