我需要将DB2查询中的布尔值转换为.NET Core中的布尔值
我有一个查询来检查正则表达式匹配的字符串。
我尝试了多种类型的转换:BOOLEAN,TINYINT,SMALLINT。
CASE WHEN (REGEXP_LIKE(TESTNAME.COL1, 'Test1')) THEN CAST(true AS BOOLEAN) ELSE CAST(false AS SMALLINT) END as Error
我的实体看起来像这样:
public class ErrorView {
[Required] public string Key { get; set; }
public bool Error { get; set; }
}
可以建立到数据库的连接,这不是问题。
但是,我得到以下内容
错误:System.InvalidCastException-无法转换类型的对象 'System.Int16'键入'System.Boolean'
。
完全相同的代码可以完美地与MySQL Server结合使用(在SQL-Query中进行了一些修改)。查询本身可以在Datagrip中使用。
编辑1: 我正在使用1.3.0.100版中的NuGet包IBM.EntityFrameworkCore。 为了进行开发,我在x64处理器上使用Windows 10(64位)。
编辑2: 这就是我所说的:
ExternalDbContextFactory dbContextFactory = new ExternalDbContextFactory(applicationDbContextOptions);
var context = dbContextFactory.Create();
var sqlRuleResult = context.EntityName.FromSql(regexRule);
我在Microsoft方法中收到转换错误。尝试在实体中手动投射它不起作用,因为它没有达到该点。
Stacktrace:
在Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue [TValue](ValueBuffer和valueBuffer,Int32索引,IPPropertyBase属性)
在lambda_method(Closure,MaterializationContext)
在Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper`1.Shape(QueryContext queryContext,ValueBuffer&valueBuffer)
在Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _,布尔缓冲区)
在Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute [TState,TResult](TState状态,Func
3 operation, Func
3验证成功)在Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
在Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
答案 0 :(得分:0)
我通过不使用布尔值而是使用整数来解决了这个问题。这不是理想的,因为稍后需要在代码中进行不同的处理,但是它可以那样工作。
public class ErrorView {
[Required] public string Key { get; set; }
public int Error { get; set; }
}