oracle-databaseef-core-2.2valueconverter

Oracle & EF Core BoolToTwoValuesConverter not Converter-ing


I have created a simple BoolToTwoValuesConverter<int> so when there's a -1 in the database column it'll convert to false, and 0 converts to true:

var falseNegOneTrueZero = new BoolToTwoValuesConverter<int>(-1, 0);

I've mapped my Property with that converter:

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);

but whenever I try to use the column in my query, it chokes hard:

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)

I've also tried it the "verbose" way by specifying a conversion like this:

   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);

... to no avail. I'm running out of ideas, any assistance is appreciated.


Solution

  • Removing the .HasColumnType() statement made the problem go away. I don't like it, but it worked:

    entity.Property(e => e.IsWhatever)
         .HasColumnName("CRYPTIC_COL_NM")
         .HasConversion(falseNegOneTrueZero);