Skip to content

The data types varchar and nvarchar are incompatible in the '^' operator #35093

@miqueljuan

Description

@miqueljuan

Projection to Boolean of Equals comparator of a Enum property (that has custom ValueConverter to string) with one of possibles values of Enum translates to SQL with the new XOR ('^') operator implementation, causing 'The data types varchar and nvarchar are incompatible in the '^' operator'.

dbContext.Orders.Select(order=> new OrderModel {  Paid = order.State == OrderState.Paid }).ToList();

enum OrderState { Unpaid, Paid, ... }

class OrderStateConverter : ValueConverter<OrderState, string> 
{
  private static string ConvertToString(OrderState state)
  {
      return stateswitch
      {
          OrderState.Unpaid=> "U",
          OrderState.Paid => "P",
          ....
          _ => throw new InvalidEnumArgumentException(nameof(state), (int)state, typeof(OrderState))
      };
  }
  private static OrderState ConvertToEnum(string state)
  {
       return state.ToUpperInvariant() switch
       {
           "U" => OrderState.Unpaid,
           "P" => OrderState.Paid,
            ....
           _ => throw new InvalidEnumArgumentException(...)
       };
  }
}

Translate to SQL as

CAST([p0].[State] ^ N''S'' AS bit) AS [Paid]

EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11 10.0.22631
IDE: Visual Studio 2022 17.11.6

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions