Oracle 和 EF Core BoolToTwoValuesConverter 不是转换器
Oracle & EF Core BoolToTwoValuesConverter not Converter-ing
我创建了一个简单的 BoolToTwoValuesConverter<int>
,所以当数据库列中有 -1
时,它将转换为 false
,而 0
转换为 true
:
var falseNegOneTrueZero = new BoolToTwoValuesConverter<int>(-1, 0);
我已将我的 属性 映射到该转换器:
modelBuilder.Entity<Foo>(entity =>
{
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasColumnType(number(1,0))
.HasConversion(falseNegOneTrueZero);
// etc...
}
// example of using the property in a query
return summaries.Where(s => s.IsWhatever);
但是每当我尝试在我的查询中使用该列时,它都会很困难:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'.
at Oracle.EntityFrameworkCore.Storage.Internal.OracleBoolTypeMapping.GenerateNonNullSqlLiteral(Object value)
at Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.VisitConstant(ConstantExpression constantExpression)
我也尝试过 "verbose" 方法,指定这样的转换:
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasColumnType(number(1,0))
.HasConversion(
// 0 is true, -1 is false
entityValue => entityValue ? 0 : -1,
dbValue => dbValue == 0);
...无济于事。我 运行 没有想法,如有任何帮助,我们将不胜感激。
删除 .HasColumnType()
语句使问题消失。我不喜欢它,但它起作用了:
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasConversion(falseNegOneTrueZero);
我创建了一个简单的 BoolToTwoValuesConverter<int>
,所以当数据库列中有 -1
时,它将转换为 false
,而 0
转换为 true
:
var falseNegOneTrueZero = new BoolToTwoValuesConverter<int>(-1, 0);
我已将我的 属性 映射到该转换器:
modelBuilder.Entity<Foo>(entity =>
{
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasColumnType(number(1,0))
.HasConversion(falseNegOneTrueZero);
// etc...
}
// example of using the property in a query
return summaries.Where(s => s.IsWhatever);
但是每当我尝试在我的查询中使用该列时,它都会很困难:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'. at Oracle.EntityFrameworkCore.Storage.Internal.OracleBoolTypeMapping.GenerateNonNullSqlLiteral(Object value) at Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.VisitConstant(ConstantExpression constantExpression)
我也尝试过 "verbose" 方法,指定这样的转换:
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasColumnType(number(1,0))
.HasConversion(
// 0 is true, -1 is false
entityValue => entityValue ? 0 : -1,
dbValue => dbValue == 0);
...无济于事。我 运行 没有想法,如有任何帮助,我们将不胜感激。
删除 .HasColumnType()
语句使问题消失。我不喜欢它,但它起作用了:
entity.Property(e => e.IsWhatever)
.HasColumnName("CRYPTIC_COL_NM")
.HasConversion(falseNegOneTrueZero);