mikependon / RepoDB

A hybrid ORM library for .NET.
Apache License 2.0
1.7k stars 125 forks source link

Question: Is there any way to force RepoDb to cast enums to integers? #1151

Closed PauloHMattos closed 1 year ago

PauloHMattos commented 1 year ago

Hi,

in my company we use Dapper+PostgreSQL for many years but we are considering switching to use RepoDb. In our initial tests using RepoDb we hit a problem with the treatment of enums by npgsql that does not support writing enums as their underlying integer types. With Dapper we didn't have this problem since it always casts the enums to the underlying integer.

Is there any way to have this behaviour applied by default to all parameter enums with RepoDb? If not, would you consider this an interesting feature for RepoDb? With a little bit of guidance I could implement this and open a PR.

mikependon commented 1 year ago

Have you tried enumeration Defaut Conversion? Or, the ConversionType?

PauloHMattos commented 1 year ago

Have you tried enumeration Defaut Conversion? Or, the ConversionType?

Yes, we tried both methods that but the result are the same:

System.InvalidCastException : Can't write CLR type Inoa.Db.Test.CanExecuteCmd+TestEnum with handler type Int32Handler

  Rastreamento de Pilha: 
    Int32Handler.ValidateObjectAndGetLength(Object value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter)
    NpgsqlParameter.ValidateAndGetLength()
    NpgsqlParameterCollection.ProcessParameters(TypeMapper typeMapper, Boolean validateValues, CommandType commandType)
    NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
    NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
    NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
    DbConnectionExtension.ExecuteNonQueryAsyncInternal(IDbConnection connection, String commandText, Object param, Nullable`1 commandType, Nullable`1 commandTimeout, String traceKey, IDbTransaction transaction, ITrace trace, CancellationToken cancellationToken, Type entityType, DbFieldCollection dbFields, Boolean skipCommandArrayParametersCheck) linha 1686

It appears to be a limitation of npgsql, that dows not treat the value as an integer even when the DbType is set to Int32. ConvertionType seens exactly what I want, but I might be using wrong.

If this is something that should be working, I more than happy to work on fix. Tomorrow I will dive deep into the code and open a draft PR for you to take a look.