Base solution for your next web application
Open Closed

Using Postgres database - "Can't write CLR type System.Boolean with handler type Int64Handler" in OrganizationUnitAppService.FindUsers #6274


User avatar
0
geertveenstra created

Anyone using postgres as a database here ?

I am getting an exception "Can't write CLR type System.Boolean with handler type Int64Handler" in the OrganizationUnitAppService.FindUsers Postgres does not seem to be able to handle a type long from the User entity ID in the following line

.Where(u => !userIdsInOrganizationUnit.Contains(u.Id))

Here is the full exception from the logfile:

System.InvalidCastException: Can't write CLR type System.Boolean with handler type Int64Handler
   at lambda_method(Closure , NpgsqlTypeHandler , Object , NpgsqlLengthCache& , NpgsqlParameter )
   at Npgsql.TypeHandling.NpgsqlSimpleTypeHandler`1.ValidateObjectAndGetLength(Object value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter) in C:\projects\npgsql\src\Npgsql\TypeHandling\NpgsqlSimpleTypeHandler.cs:line 236
   at Npgsql.NpgsqlParameter.ValidateAndGetLength() in C:\projects\npgsql\src\Npgsql\NpgsqlParameter.cs:line 553
   at Npgsql.NpgsqlCommand.ValidateParameters() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 793
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1140
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken) in C:\projects\npgsql-entityframeworkcore-postgresql\src\EFCore.PG\Storage\Internal\NpgsqlExecutionStrategy.cs:line 49
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.AsyncQueryMethodProvider.GetResult[TResult](IAsyncEnumerable`1 valueBuffers, Boolean throwOnNullResult, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.TaskResultAsyncEnumerable`1.Enumerator.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteSingletonAsyncQuery[TResult](QueryContext queryContext, Func`2 compiledQuery, IDiagnosticsLogger`1 logger, Type contextType)
   at Portal.Organizations.OrganizationUnitAppService.FindUsers(FindOrganizationUnitUsersInput input) in C:\Users\Administrator\Dropbox\Development\DotNet\Workspaces\Websites\Portal\src\Portal.Application\Organizations\OrganizationUnitAppService.cs:line 159
   at lambda_method(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()
  

As a workaround I change the long type to an int type (which off course is not very nice).

Any suggestions to prevent this ?


7 Answer(s)
  • User Avatar
    0
    geertveenstra created

    If I convert it explicitly to Int64 the exception is also not happening.

    .Where(u => !userIdsInOrganizationUnit.Contains(Convert.ToInt64(u.Id)))

    instead of

    .Where(u => !userIdsInOrganizationUnit.Contains(u.Id))

    I always thought there was no difference between using long or Int64 types ?

  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, did you change anything inside the FindUsers()?

    If possible, please share the code for the method.

    Also can you try the following so that we can be sure that the first where clause is the root cause ?

    var query = UserManager.Users.Where(u => !userIdsInOrganizationUnit.Contains(u.Id));
    var userCount = await query.CountAsync();
    
  • User Avatar
    0
    geertveenstra created

    Hi, nothing was changed inside the FindUsers().

    Your code is also giving the same Exception.

    var query = UserManager.Users.Where(u => !userIdsInOrganizationUnit.Contains(u.Id));
    var userCount = await query.CountAsync();
    

    If I change the code to

    var query = UserManager.Users.Where(u => !userIdsInOrganizationUnit.Contains(Convert.ToInt64(u.Id)));
    var userCount = await query.CountAsync();
    

    it is working fine and the non-assigned users show up in the modal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you share the definition of userIdsInOrganizationUnit ? Might be related to https://github.com/npgsql/npgsql/issues/443

  • User Avatar
    0
    geertveenstra created

    Sure, it is still the original code.

    var userIdsInOrganizationUnit = _userOrganizationUnitRepository.GetAll() .Where(uou => uou.OrganizationUnitId == input.OrganizationUnitId) .Select(uou => uou.UserId);

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks.

    Are you using Npgsql ? If so, you can ask this on their repository. I think it is related to Npgsql.

  • User Avatar
    0
    geertveenstra created

    I am using Npgsql yes.

    i will give it a try, thanks for your effort !