How to resolve casting Error in DB2 Boolean to .NET Core Boolean?

.net .net-core c# db2 entity-framework-core

Question

I need to cast Booleans from a DB2 Query to Booleans in .NET Core

I've got a query to check wheter a string matches with a Regex.

I tried multiple casts: BOOLEAN, TINYINT, SMALLINT.

CASE WHEN (REGEXP_LIKE(TESTNAME.COL1, 'Test1')) THEN CAST(true AS BOOLEAN) ELSE CAST(false AS SMALLINT) END as Error

My Entity looks something like that:

public class ErrorView {
    [Required] public string Key { get; set; }
    public bool Error { get; set; }
}

A connection to the database can be established, this is not the problem.

However, I get the following

Error: System.InvalidCastException - Unable to cast object of type 'System.Int16' to type 'System.Boolean'

.

The exact same code works perfectly combined with a MySQL Server (given some adaptations in the SQL-Query). The Query itself works in Datagrip.

Edit 1: I'm using the NuGet Package IBM.EntityFrameworkCore in Version 1.3.0.100. And for developing I use a Windows 10 (64 Bit) on a x64-Processor.

Edit 2: This is what I call:

ExternalDbContextFactory dbContextFactory = new  ExternalDbContextFactory(applicationDbContextOptions);
var context = dbContextFactory.Create();
var sqlRuleResult = context.EntityName.FromSql(regexRule);

I get the casting error in a Microsoft-Method. Trying to cast it manually in the Entity does not work since it does not reach that point.

Stacktrace:

at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue[TValue](ValueBuffer& valueBuffer, Int32 index, IPropertyBase property)

at lambda_method(Closure , MaterializationContext )

at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)

at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)

at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)

at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()

at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

1
0
10/13/2019 1:56:10 PM

Popular Answer

I solved it by simply not using a boolean but an integer. It's not ideal since it requires different handling later on in the code, but it works that way.

public class ErrorView {
    [Required] public string Key { get; set; }
    public int Error { get; set; }
}
0
1/8/2020 6:09:18 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow